Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you access the DOM node in jquery inside of a javascript variable?

I have the following html

<div id="list_item_template"><li><a href="#">Text</a></li></div>

and javascript:

var item = $("#list_item_template").clone();

What I want to do is access the inner <a> tag of the cloned copy and add an attribute. Without cloning I would just do:

$("#list_item_template a").attr("onclick", "SomeFunction()");

However, I need to perform that operation on the cloned copy, not on the html currently on the page. How would I do this?

like image 253
KallDrexx Avatar asked Mar 29 '26 04:03

KallDrexx


2 Answers

item.find('a'); should do it.

like image 163
x1a4 Avatar answered Apr 02 '26 04:04

x1a4


$("#list_item_template a").attr("onclick", "SomeFunction()");

is not advisable... read this...

use .click() instead...

$("a",item).click(SomeFunction);
like image 21
Reigel Avatar answered Apr 02 '26 02:04

Reigel



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!