rcsnooks rcsnooks - 5 months ago 10
jQuery Question

Document on click using selector

How to get the containing item using document on click.

var this_main = $(this);
$(document).this_main.on('click', '.nav-circlepop a.prev', function(){
});


What am I doing wrong? Thanks!

Answer

You can use text method to grab the text .

Since you have not provided any HTML I presume the below HTML snippet is close to your code

HTML

<div class = "nav-circlepop">
<a class = "prev" href=""> Anchor tag text </a>
</div>

jQuery

$(document).on('click', '.nav-circlepop a.prev', function(event){
    event.preventDefault();
     document.write('<pre>'+$(this).text()+'</pre>')
});

Code Explanation

$(document).on('click', '.nav-circlepop a.prev',function(event){})

Any click event on document will be delegate to the target element.

Here the second parameter represent the target element.

where .nav-circlepop is a parent element and anchor tag a is child element. This anchor tag a has a class prev .So the target element is a.prev

Check this DEMO

Comments