0

I have these data-attributes

<li data-audio="" data-pic="images/one.png" data-word="one" data-hint="What?"></li>

I pull them through using this function

$(wordsData).each(function () {
    var elm = $(this);
    listOfWords.push({
        "name": elm.data("word"),
        "pic": elm.data("pic"),
        "hint": elm.data("hint"),
        "audio": elm.data("audio")
    });
});

My problem is that when I pull the picture through I would like to display the hint in the same div. But for some reason when I check in the console it says value="", when it should say "What?"

<div class="hint-img-wrapper">
<img src="" value="" id="hintPic" class="pic-hint" alt="Hint" />

I display it it like this

 $("#hintPic").attr('src', listOfWords[rndWord].pic).attr('value', listOfWords[rndWord].hint);
        $(hintPic).show();

Can someone tell me what I am doing wrong?

9
  • 4
    value is not a valid attribute on an <img> tag. Use an HTML5 data- attribute like your other code. What are you trying to accomplish with the value attribute there anyway? Commented Nov 7, 2012 at 14:49
  • Code seems correct, which version of jQuery did you use? Commented Nov 7, 2012 at 14:50
  • So I have changed value for title, will this work? If so what do I cange in the markup? Commented Nov 7, 2012 at 14:55
  • If you inspect value attribute from console and get nothing, it must be a problem with code and not markup or anything else, try to step-by-step debug this code, to see if rndWord is a correct number value that could retrieve a valid data object from listOfWords? Commented Nov 7, 2012 at 15:00
  • 1
    @Milo-J It's better you post the answer yourself since I don't know what exactly the problem is and how you finally solved it :) Commented Nov 8, 2012 at 3:14

1 Answer 1

1

What you really want to set instead of .attr('value', listOfWords[rndWord].hint); is .attr('title', listOfWords[rndWord].hint);

The title property is what's normally displayed if you hover over an image. If no title is specified it will use the alt property.

Sign up to request clarification or add additional context in comments.

5 Comments

what would I change in the Html @Sargo Darya
You don't need to change anything in the HTML. The last snippet of code is just a bit wrong. You're setting value (which is not even a valid property for the img tag) instead of the title property.
well it still doesn't work. Where else would it say title? @Sargo Darya
what would <div class="hint-img-wrapper"> <img src="" value="" id="hintPic" class="pic-hint" alt="Hint" /> change to
If you want to display the text directly you should change the markup to <div class="hint-img-wrapper"> <img src="" value="" id="hintPic" class="pic-hint" alt="Hint" /><span class="hintText"></span></div> and in the javascript code you put $('.hint-img-wrapper').find('.hindText').text(listOfWords[rndWord].hint)'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.