I have html form like this: How can I select Gray / Silver from data-title?
<div class="value">
<div class="color-box grey-silver-color-gradient" data-title="Grey / Silver" data-toggle="tooltip" data-original-title="" title=""></div>
</div>
I have wrote my code but not get the result. here
var data = $('.value').data('title');
console.log(data);
Pls help me. Thanks in advace
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.
You can use the attribute selector: $('[data-my-key]').
The jQuery. hasData() method provides a way to determine if an element currently has any values that were set using jQuery. data() . If there is no data object associated with an element, the method returns false ; otherwise it returns true .
getElementsByTagName('a'), for example, selects all anchor tags (links) on a page); and some browsers support methods for selecting all elements with a particular class or using a CSS selector to select page elements.
Your problem is the data-title
is on the child element to .value
.
try:-
var data = $('.value > .color-box').data('title');
console.log(data);
or
var data = $('.color-box').data('title');
console.log(data);
You should do
var data = $(".color-box.grey-silver-color-gradient").data("title");
console.log(data);
You can get attribute from tag by using attr
from jquery
Example:
$('*[userattribute]').attr('userattribute');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With