SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Getting image's alt text using javascriot

    Hi,

    I want to get image's alt text using javascript. Bascially i have a php script in which i can specify images with alt text.

    Now i have to display text in a div under the image. And the script is using img change on mouse over so there are some tiny thumbnails on which when you take the cursor the main big image changes to that.

    I want the same to be done with thumbnail's alt text. In the div the text should update when the cursor is taken to any thumbnail.

    Please help.

    Thanks.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Something like the following should do what you're after.

    Code javascript:
    function handleOnMouseOver() {
        // set the mouseOver image
        setOverImage(this);
        // set the image text
        setImageText(this);
    }
    function setImageText(img) {
        // update div. If the image has an identifier of "myImage"
        // then this will update the div called "myImageText"
        var el = document.getElementById(img.id + 'Text');
        var text = document.createTextNode(img.alt);
        while (el.firstChild) {
            el.removeChild(el.firstChild);
        }
        el.appendChild(text);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    HI,

    Thanks for the code. I tried this and it worked:

    <img src="abc.gif" alt="Some Text" onmouseover="document.getElementById('ImgText').innerHTML ='Some Text';">

    Thaks.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    I'm somewhat at a loss to express how I feel that all due care and attention has been put by the wayside, but yes, for a limited value of "works" - it does.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •