Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get values from label using jQuery

Tags:

jquery

label

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 &nbsp;2010</label>
like image 286
Pankaj Avatar asked Jun 11 '10 07:06

Pankaj


People also ask

How do I get text from labels?

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.

Can HTML label have a value?

<input type="month"> <input type="number"> <input type="password"> <input type="radio">


2 Answers

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 &nbsp;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();
like image 139
Gavin Mogan Avatar answered Sep 28 '22 19:09

Gavin Mogan


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
like image 28
icktoofay Avatar answered Sep 28 '22 20:09

icktoofay