Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select element with specific data by using jQuery

Is it possible to set some data by using the jQuery data method and then later query it? Something like ... find all elements with data foo == true?

like image 804
tok Avatar asked Dec 20 '10 17:12

tok


People also ask

How do I target a specific element in jQuery?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

How do I select an element with a data attribute?

Use the querySelector method to get an element by data attribute, e.g. document. querySelector('[data-id="box1"]') . The querySelector method returns the first element that matches the provided selector or null if no element matches the selector in the document.

How do I select data target?

Right-click the new target and select Properties. The attributes of the target open in a new pane. Select an Adapter from the drop-down menu to specify the target type. Note: Only data target types that are configured on the server and supported by DataMigrator will appear.

How can I select an element by name with jQuery?

How to select an element by name with jQuery? The JavaScript getElementsByName() method can be used to select the required element and this can be passed to a jQuery function to use it further as a jQuery object.


1 Answers

You can use .filter() to select the elements you want, like this:

$("someSelector").filter(function() { return $.data(this, "foo"); });

In this case since you're checking a boolean it's very simple, for checking against a value just add the comparison, like this:

$("someSelector").filter(function() { return $.data(this, "foo") == "value"; });
like image 166
Nick Craver Avatar answered Sep 24 '22 07:09

Nick Craver