Reputation: 184
I can get all entries' attribute like style display in the div. but I can't get the data-value from the same way. I need find a correct syntax to get the all data-value entries.
$.ajax({
url:"{{ url_for('fetch_bar') }}",
type:"post",
dataType: "json",
async: true,
success:function(response){
var response = JSON.parse(response);
var len = Object.keys(response).length;
var selects = document.querySelectorAll("select[id=select_host]");
var bars = document.querySelectorAll("[id=ldBar]");
if (len == 1) {
....
}
else if (len > 1)
for( var ind = 0; ind < len; ind++){
for (var index = 0; index < selects.length ; index++)
if (selects[index].value == response[ind].hostname)
// alert(bars[index].style.display)
alert(bars[index].data-value)
// alert(bars[index].[data-value])
<td>
<input id="stage" type="button" class="btn-primary" onclick="readTextFile();" name="stage" value="{{entry.stage}}" >
<div id="ldBar" class="ldBar label-center" data-value="0" data-preset="stripe" data-aspect-ratio="none" style="width:100%;display: block;" ></div>
</td>
<td>
Upvotes: 1
Views: 14564
Reputation: 72
HTML CODE:
<td>
<input id="stage" type="button" class="btn-primary" onclick="readTextFile();" name="stage" value="{{entry.stage}}" >
<div id="ldBar" class="ldBar label-center" data-value="0" data-preset="stripe" data-aspect-ratio="none" style="width:100%;display: block;" ></div>
</td>
JS CODE:
var abc = $('td').find('#ldBar').attr('data-value');
Upvotes: 0
Reputation: 53
If you have event then you can simply use it by:
const { dataset: { value }} = e.target;
or you can directly get it by using JQuery:
$("#your_element_id").data("value")
Upvotes: 1
Reputation: 4246
You can usedataset
, like this:
const ldBar = document.getElementById("ldBar")
console.log(
ldBar.dataset.value,
ldBar.dataset.preset,
ldBar.dataset.aspectRatio // Note the conversion to camelCase
);
<div
id="ldBar"
class="ldBar label-center"
data-value="0"
data-preset="stripe"
data-aspect-ratio="none"
style="width:100%; display: block;"
></div>
(See: https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes)
Upvotes: 0
Reputation: 5960
If you want to get data-value
of the element with jQuery:
console.log($("div").data("value"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-value=10><div>
Vanilla JS:
console.log(document.querySelector('div').getAttribute("data-value"));
<div data-value=10></div>
Dataset:
console.log(document.querySelector('div').dataset.value);
<div data-value=10><div>
Upvotes: 1
Reputation: 1368
To get the data
attribute from an element using jQuery use this syntax:
$("#your_element_id").data("value")
Just note that if you write it like this in your html:
<div id='test' data-testID='1'></div>
In jQuery you will access it like this:
$("#test").data("testid");
As all the data properties are returned as lowercase
Upvotes: 0
Reputation: 12152
Use .getAttribute("data-value")
to get the value
console.log(document.querySelector('#ldBar').getAttribute("data-value"))
<div id="ldBar" class="ldBar label-center" data-value="0" data-preset="stripe" data-aspect-ratio="none" style="width:100%;display: block;" ></div>
Upvotes: 2