I want to get month and year value from label. How can i get these using jquery?
<label year="2010" month="6" id="current Month"> June 2010</label>
Use the textContent property to get the text of a label element, e.g. const text = label. textContent . The textContent property will return the text content of the label and its descendants.
<input type="month"> <input type="number"> <input type="password"> <input type="radio">
Firstly, I don't think spaces for an id is valid.
So i'd change the id to not include spaces.
<label year="2010" month="6" id="currentMonth"> June 2010</label>
then the jquery code is simple (keep in mind, its better to fetch the jquery object once and use over and over agian)
var label = $('#currentMonth');
var month = label.attr('month');
var year = label.attr('year');
var text = label.text();
You can use the attr
method. For example, if you have a jQuery object called label
, you could use this code:
console.log(label.attr("year")); // logs the year
console.log(label.attr("month")); // logs the month
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