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
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
Share Improve this question asked Dec 16, 2015 at 8:28 Doo DooDoo Doo 1,3472 gold badges11 silver badges28 bronze badges6 Answers
Reset to default 5Your 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');
You can select it by jquery method like this
$(selector).data('name');
in your case like this
var data = $('.value > .color-box').data('name');
You are selecting one element and then expecting to get a property of another element. $('.value')
selects the parent div of the one you want to select.
You should try this, keeping specific to a child of $('.value')
:
var data = $('.value .color-box.grey-silver-color-gradient').data("title");
console.log(data);
or any child where data-title attribute has a value:
var data = $(".value [data-title!='']").data("title");
console.log(data);
jsfiddle: https://jsfiddle.net/o3ffptj3/1/
You can use attr()
var data = $('.value div').attr('data-title');