jQuery trick: get elements by any attribute

In jQuery coding we are used to get elements by id or class name.

1
2
3
jQuery("#element_id")....
 
jQuery(".element_class_name")....

But it is also possible to get elements by any attribute name.

For example lets get element which name is ‘element_name’

1
jQuery('[name="s"]')....

Or by any custom non-standard attribute.

1
jQuery('[data-image="any_image_src"]')....

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.