开发者

Get values from label using jQuery

I want to get month and year value from label. How can i get these usi开发者_StackOverflowng jquery?

<label year="2010" month="6" id="current Month"> June &nbsp;2010</label>


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();


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


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 &nbsp;2010</label>


I am changing your id to current-month (having no space)

alert($('#current-month').attr('month'));
alert($('#current-month').attr('year'));


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 &nbsp;2010</label>


Try this:

var label = $('#currentMonth').text()


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 &nbsp;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(),
    month = labelText.match(/^[a-z]+/i)[0],
    year = labelText.match(/\d{2,4}$/)[0];

console.log(month, year);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label year="2010" month="6" id="currentMonth"> June &nbsp;2010</label>

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'),
    month = label.data('month'),
    year = label.data('year');

console.log(month, year);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label data-year="2010" data-month="6" id="currentMonth"> June &nbsp;2010</label>

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'],
    label = $('#currentMonth'),
    month = monthNames[+label.data('month') - 1],
    year = label.data('year');

console.log(month, year);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label data-year="2010" data-month="6" id="currentMonth"> June &nbsp;2010</label>

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'],
    label = document.getElementById('currentMonth'),
    month = monthNames[+label.dataset.month - 1],
    year = label.dataset.year;

console.log(month, year);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label data-year="2010" data-month="6" id="currentMonth"> June &nbsp;2010</label>

References:

  • JavaScript:
    • HTMLElement.dataset.
    • Regular Expressions Guide.
    • String.prototype.match().
    • String.prototype.trim().
  • jQuery:
    • data().
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜