Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
42
rated 0 times [  49] [ 7]  / answers: 1 / hits: 15056  / 9 Years ago, thu, december 24, 2015, 12:00:00

I have few link elements related to a certain class and assigned row attributes to it and I'm trying to trigger click event based on the attribute value.





<a class=manage_edit_nb nb_id=1></a> | <a class=manage_del_nb nb_id=1></a>
<a class=manage_edit_nb nb_id=2></a> | <a class=manage_del_nb nb_id=2></a>
<a class=manage_edit_nb nb_id=3></a> | <a class=manage_del_nb nb_id=3></a>...





Is it possible to trigger click or any event for a certain attribute value for a certain class?



If I try something similar to





$('a[nb_id = 1]').trigger('click');





it triggers click event for all elements irrespective of class but I failed to figure out how to put class reference in there!


More From » jquery

 Answers
111

First, the nb_id is not a valid attribute, use data-id instead. data-* attributes are allowed, and I personally like them. And, they can be accessed using $.attr('data-id') method, and their value can bee updated using $.attr('data-id', 'new value'). Going back to the question, try using below selector



$('.manage_del_nb[data-id=1]').get(0).click();


OR



$('.manage_edit_nb[data-id=1]').get(0).click();


Why .get(0)? Assuming that the element has been bound with .click(callback()) or .on('click'), the .trigger('click') will not do anything, so I am using .get(0) to get the DOM object which has that method to simulate the click event. Regardless of being said, you can use trigger('click') the way you're already using


[#63952] Tuesday, December 22, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shantelc

Total Points: 737
Total Questions: 120
Total Answers: 104

Location: Nicaragua
Member since Tue, Dec 8, 2020
4 Years ago
;