0

This is part of another query I have here but really stuck....

I cant get jquery clcik event to work on some ajax generated HTML.

I've read all the articles and questions/answers but she wont work for me .. all help greatly appreciated....

JS

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

$(document).ready(function(){    
    $("document").on("click", ".clickme", function() {
        alert("click!");
    });

ajax

$.ajax({

        url: 'http://www.websites.com/lol.php',

        dataType: 'jsonp',

        jsonp: 'jsoncallback',

        timeout: 5000,

        success: function(data, status){            
            $.each(data, function(i,item){    
                 var balance = '<p>'+item.itemm + ' '+ item.amount + ' '+item.status  +  ' ' + '<input value="delete "type="button" id="clickme">'
                 + '</p><hr>';
                total = total + parseInt(item.amount);

                $("#mylbl2").append(balance);

HTML

<div class="col span_1_of_3">
   <b>VISA</b>
   <span id="mylbl2"></span>
   <b>TOTAL: <span id="mylbl3"></span></b><br/><br/>
</div>

Currently all I'm trying to do is get a click event working on the delete button...once I can get this working I can move on get the id and pass to a delete function... I'm stuck on getting the click event working.

Could it just be punctuation or something silly that I'n overlooking??

3 Answers 3

4

document is not a selector. So, it shouldn't be wrapped in " ". Do the following

$(document).on("click", "#clickme", function() {
    alert("click!");
});
Sign up to request clarification or add additional context in comments.

4 Comments

Also, clickme is the id of the element, not the class so the selector should be #clickme
@RoryMcCrossan was too lazy to scroll ;) Thanks anyway
Thank you...resolved...it was the # that sorted it....Now I get my life back.... thanks again all
@SeanF glad to have helped you
1

try this

$(document).ready(function(){    
    $(document).on("click", "#clickme", function() {
        alert("click!");
    });
});

1 Comment

@SeanF it was id, # should be used as in my answer
0

Are you trying to add a click listener to the class of the element? Cause in your code you're adding it to the CLASS while it seems like you're referring to the ID. If you are willing to add multiple items with only the id change, it might be better to do:

$(".clickme").click(function(e) 
{
    alert(e.target.id);
});

And change in your ajax call

<input value="delete "type="button" id="clickme">

to

<input value="delete "type="button" class="clickme">

Comments

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.