I just wonder if is it posible to have option elements in html with data- attributes and so, if that posible how to retrive this value.
More specific. Lets say I have the following code:
<select name="areas" id="areas">
<option value="1" data-lat="12.243" data-lng="32.43242">Area name</option>
<option value="2" data-lat="14.243" data-lng="34.43242">Area name</option>
<option value="3" data-lat="16.243" data-lng="36.43242">Area name</option>
<option value="4" data-lat="18.243" data-lng="38.43242">Area name</option>
</select>
then, is it any way to get the data-lat and data-lng for from the selected option via jQuery ?
To get a data attribute through the dataset object, get the property by the part of the attribute name after data- (note that dashes are converted to camelCase). Each property is a string and can be read and written.
An <option> element can have any number of data-* attributes, each with their own name. Using data-* attributes reduces the need for requests to the server. Note: The data-* attribute is not visible and does not change the appearance of the option.
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. Here is the HTML for the examples in this article.
Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to find the data-id attribute of an HTML element.
Yes that's possible. Any attribute value can be get by:
$('option:selected').attr('data-lng')
and set by:
$('option:selected').attr('data-lng', 1234);
Set attribute using setAttribute
:
document.getElementById('areas')
.getElementsByTagName('option')[index]
.setAttribute('data-lng', 'foo');
Get attribute using getAttribute
:
document.getElementById('areas')
.getElementsByTagName('option')[index]
.getAttribute('data-lng');
Both get and set:
var el = document.getElementById('areas')
.getElementsByTagName('option')[index];
el.getAttribute('data-lng');
el.setAttribute('data-lng', 'foo');
Note 1 I have used document.getElementById('areas').getElementsByTagName('option')[index]
because probably is more cross-browser, but you could use
document.getElementById('areas').getElementsByTagName('option')[index]
document.getElementById('areas').options[index]
document.getElementById('areas')[index]
Note 2 I have used setAttribute
and getAttribute
because they are more cross-browser, but you could also use dataset:
el.dataset.lng; // get
el.dataset.lng = 'foo'; // set
You can use .data
to retrieve data attributes from elements
$('#areas options').each(function(){
console.log($(this).data('lat'), $(this).data('lng'));
}
The advantage of this over .attr
is this is that you get type conversion so you'll get a Numeric value in this case rather than a string.
I'd like to add, If you want to target a specific <select>
You can also pass the container element so that <options>
from only that element will be selected.
// For user with events like .on("change") etc.
console.log($("option:selected", this).attr("data-lat"));
OR
console.log($("option:selected", "#areas").attr("data-lat"));
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