tags:

views:

69

answers:

3

How to get the value of span tag based on css class in jquery?

<span class="page-numbers current">3</span>

I used

var pageno = $(".page-numbers current").val();
  alert(pageno);

it doesnt seem to work.any suggestion..

EDIT:i am using jquery pagination plugin for pagination... for first page 1 and prev are assigned css class class="page-numbers current

<span class="page-numbers current prev">Prev</span>
<span class="page-numbers current">1</span>
A: 

I assume only the current page was the class .current so grab it using only that selector.

Grab it using jQuery .html() like this:

// do notice that a . was added to class .current!
var pageno = $(".current").html();
alert(pageno);


You were trying to capture the val that exists in fields like input.

// on a field like this what you were doing would work perfectly
<input type='text' val='12' />
Frankie
+1  A: 

".page-numbers" and "current" are 2 different css classes.

Your jQuery expects "current" to be inside of ".page-numbers".

Also use text() for non-input elements.

This should work

alert($(".page-numbers").text());

EDIT: Furthermore, current hasn't got a class (.) or id (#) prefix which means it's looking for an element "current", which doesn't exist. Use #current or .current

Marko
i want to get current page number
bala3569
If class="current" only appears on one element, simply use $(".current").text()
Marko
@bala - you could `$(".page-numbers.current").text()`....
Reigel
for first page i get `prev1` and for the last page i get `3next` all highlighted
bala3569
+1  A: 
var pageno = $(".page-numbers.current").text();
  alert(pageno);

see second example class-selector

Reigel
for first page i get `prev1` and for the last page i get `3next` all highlighted
bala3569
@bala - I don't understand... there's something you have not explained in your html structure... can you edit your post with more detail of the problem...
Reigel
@Reigel Look at my edit
bala3569
as I thought... why do you have `current` on on the other `span`... isn't it should be on one `span`? I mean, when you are in page 1, `<span >1</span>` should have a `class` `current` and others should not... should it really has to be that some others like `<span>Prev</span>` should have a `class` `current`?
Reigel