tags:

views:

103

answers:

4

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>
+1  A: 

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

alert($('#current-month').attr('month'));
alert($('#current-month').attr('year'));
Khnle
Thanks @@Khnle..
Pankaj
@Pankaj - You're welcome.
Khnle
+1  A: 

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
icktoofay
+1  A: 

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>
Neb
+2  A: 

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();
halkeye
Spaces for an ID not valid, yeah... well you're putting unknown attributes on a label; that's not valid either, is it? (I guess it's more accepted though...)
icktoofay