Reputation: 1487
I have some markup like so:
<div id="mydiv">
<span></span>
<ul>
<li>1</li>
<li>2 <a xyz="something"></a></li>
</ul>
</div>
I want to bind click event to the <A>
tag, like this:
$("#mydiv A").click(this.function(){});
Can it be better? Thanks!
Upvotes: 0
Views: 49
Reputation: 46873
If you need to select a specific element, it's always faster to select it directly by id. For example:
<a id="myAnchor" xyn="something"></a>
$(#myAnchor).click(function() { alert('sup stallion');});
If however you want to assign a click event for all anchor tags under #mydiv
then your approach is fine.
Upvotes: 0
Reputation: 1985
That works .. you could define an ID for the tag and select it directly - that's the fastest selecting method.
Upvotes: 0
Reputation: 1929
I would give the <a>
tag an id and then attach a click listener like this:
$("#id_of_a_element").click(function() {
alert("yep you clicked it");
});
This will give you greater accuracy under the div, on the other hand your method should work.
Upvotes: 0
Reputation: 50513
Sure assign an id
to the <a>
<a id="mylink" xyz="something"></a>
$("#mylink").click(this.function(){});
Upvotes: 1