Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select elements inside data-title with jQuery?

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

like image 321
Doo Doo Avatar asked Dec 16 '15 08:12

Doo Doo


People also ask

How to find element with data attribute jQuery?

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.

Which jQuery selector is used to retrieve all elements with the attribute data attribute name?

You can use the attribute selector: $('[data-my-key]').

How do you check if data attribute exists in jQuery?

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 .

Which jQuery statement selects all anchor tags within paragraphs on a page?

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.


3 Answers

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);
like image 103
BenG Avatar answered Oct 20 '22 00:10

BenG


You should do

var data = $(".color-box.grey-silver-color-gradient").data("title");
console.log(data);
like image 38
void Avatar answered Oct 19 '22 23:10

void


You can get attribute from tag by using attr from jquery

Example:

$('*[userattribute]').attr('userattribute');
like image 21
Brane Avatar answered Oct 19 '22 23:10

Brane