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
I am changing your id to current-month (having no space)
alert($('#current-month').attr('month'));
alert($('#current-month').attr('year'));
Try this:
var label = $('#currentMonth').text()
Use .attr
$("current_month").attr("month")
$("current_month").attr("year")
And change the labels id to
<label year="2010" month="6" id="current_month"> June 2010</label>
var label = $('#current_month');
var month = label.val('month');
var year = label.val('year');
var text = label.text();
alert(text);
<label year="2010" month="6" id="current_month"> June 2010</label>
While this question is rather old, and has been answered, I thought I'd take the time to offer a couple of options that are, as yet, not addressed in other answers.
Given the corrected HTML (camelCasing the id
attribute-value) of:
<label year="2010" month="6" id="currentMonth"> June 2010</label>
You could use regular expressions to extract the month-name, and year:
// gets the eleent with an id equal to 'currentMonth',
// retrieves its text-content,
// uses String.prototype.trim() to remove leading and trailing white-space:
var labelText = $('#currentMonth').text().trim(),
// finds the sequence of one, or more, letters (a-z, inclusive)
// at the start (^) of the string, and retrieves the first match from
// the array returned by the match() method:
month = labelText.match(/^[a-z]+/i)[0],
// finds the sequence of numbers (\d) of length 2-4 ({2,4}) characters,
// at the end ($) of the string:
year = labelText.match(/\d{2,4}$/)[0];
var labelText = $('#currentMonth').text().trim(),_x000D_
month = labelText.match(/^[a-z]+/i)[0],_x000D_
year = labelText.match(/\d{2,4}$/)[0];_x000D_
_x000D_
console.log(month, year);
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<label year="2010" month="6" id="currentMonth"> June 2010</label>
_x000D_
Rather than regular expressions, though, you could instead use custom data-*
attributes (which work in HTML 4.x, despite being invalid under the doctype, but are valid under HTML 5):
var label = $('#currentMonth'),_x000D_
month = label.data('month'),_x000D_
year = label.data('year');_x000D_
_x000D_
console.log(month, year);
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<label data-year="2010" data-month="6" id="currentMonth"> June 2010</label>
_x000D_
Note that this will output 6
(for the data-month
), rather than 'June'
as in the previous example, though if you use an array to tie numbers to month-names, that can be solved easily:
var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],_x000D_
label = $('#currentMonth'),_x000D_
month = monthNames[+label.data('month') - 1],_x000D_
year = label.data('year');_x000D_
_x000D_
console.log(month, year);
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<label data-year="2010" data-month="6" id="currentMonth"> June 2010</label>
_x000D_
Similarly, the above could be easily transcribed to the native DOM (in compliant browsers):
var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],_x000D_
label = document.getElementById('currentMonth'),_x000D_
month = monthNames[+label.dataset.month - 1],_x000D_
year = label.dataset.year;_x000D_
_x000D_
console.log(month, year);
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<label data-year="2010" data-month="6" id="currentMonth"> June 2010</label>
_x000D_
References:
Source: Stackoverflow.com