0

I have a script that produces a number of buttons with a class and I want it to alert the data attribute on click but it's not working.

Here is the output of HTML

<button class="request box-button" data-value="18492500814">Request</button>

jQuery code

$(document).ready(function(){
    $('.request').each(function () {
        var photoID = $(this);
        photoID.click(function () {
            alert($(this).data('value'));
        });
    });
});
1
  • it seems to work pretty fine, see this fiddle: jsfiddle.net/2s3ngvd3 what jquery version you are using? Is there anything else which might be affecting your problem? what's the error message you get? edit ah nvm, overseen the fact that you are using a script to create the buttons themselves. see blex' answer, then :) Commented Jul 14, 2015 at 15:09

3 Answers 3

6

Since your elements don't exist when the page loads, the event won't be bound to them. Fix that by using event delegation:

$(document).ready(function(){
    $(document).on('click','.request', function () {
            alert($(this).data('value'));
    });
});

JS Fiddle demo with dynamically generated elements

Note: Here, I used $(document).on() because I don't have your page's structure. But if you insert the buttons in a container that already exists in your HTML, use this instead: $('#myContainer').on(). It won't be noticeable, but it is best for performance.

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

4 Comments

Ahh you gentlemen! I knew it was something to do with this because when I click "view source" there are no relevant elements. So, essentially what you've done with the above is scan the document a second time when using the click event? (poorly explained!)
@WebDevDanno Well, not really. What it says is: "When there is a click on any element of the document, if it corresponds to the selector '.request', then, execute this function." That way, even if elements are added later, they will be ckecked and used.
I see! Thank you, jQuery logic is something I'm currently struggling with. I'm glad there's nice folk out there who can help out! =]
I like your exemplary sportsmanship. I wish there was more users like you
0

Why not just have the listener on request, instead of inside of the loop. Also use the attr to get the data-value

$(document).ready(function(){
    $('.request').click(function () {
        alert($(this).attr('data-value'));
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<button class="request box-button" data-value="18492500814">Request</button>

1 Comment

jQuery has a handy tool to obtain values from data- attributes. So it is actually cuter to use the 'data' function. You are right about removing the loop though!
0

Try with attr method.

$(document).ready(function(){
    $('.request').each(function () {
        var photoID = $(this);
        photoID.click(function () {
            alert($(this).attr('data-value'));
        });
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<button class="request box-button" data-value="18492500814">Request</button>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.