KallDrexx
KallDrexx

Reputation: 27813

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?

Upvotes: 3

Views: 114

Answers (2)

Reigel Gallarde
Reigel Gallarde

Reputation: 65264

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

is not advisable... read this...

use .click() instead...

$("a",item).click(SomeFunction);

Upvotes: 2

x1a4
x1a4

Reputation: 19485

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

Upvotes: 3

Related Questions