Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unidentified problem with jQuery's .click() function

Tags:

jquery

I'm trying to add an 'onclick' function to an image with jQuery and for some reason it's not sticking. My code looks like:

//create arrow icon/button for li
var img = $('<img></ >').attr('id',i+5)
                       .attr("src","images/16-arrow-right.png")
                       .addClass("expImg")
                       .click(function(){expand(this, 'tdb', 'years', 'danwoods')})
                       .appendTo(li);   

and the resulting element looks like;

<img id="6" src="images/16-arrow-right.png" class="expImg"/>

It completely leaves out the onclick function, but everything else works fine. Does anyone see a problem with my code, or does the error lie elsewhere? Thanks in advance...

like image 628
danwoods Avatar asked Nov 27 '25 20:11

danwoods


1 Answers

I wouldn't expect the click event handler you're defining to show up in an HTML rendering of the element. You're really not "setting an onclick"; you're binding a function to the DOM click event, which is kind of a different thing. For starters, you can bind several such functions and they play nicely with each other.

If it's really important to you that it render, try doing:

.attr('onclick', "expand(this, 'tdb', 'years', 'danwoods')")

and see if that works for you.

like image 122
chaos Avatar answered Nov 30 '25 09:11

chaos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!