Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: selecting a span with a particular id

Tags:

jquery

I'm getting an error message on this function telling me that parseInt doesn't have a radix parameter, even though I'm passing it the variable moveCount. I assume this means that the value of the span with the id #moveCount is not being assigned to the variable moveCount. I've marked the line below where I think the problem's arising. I've tried this line

moveCount = $('span #moveCount').html();

both with and without using the word 'span' in the selector and am getting the same result.

function incrementMoveCount() {
    //gets the html of the span with id
    //moveCount
    //turns it into a number
    //increments it by one
    //sets the html of the span with id moveCount
    //to the new move count
    var moveCount = 0; 
    var newMoveCount = 0;
    moveCount = $('span #moveCount').html();   # problem here
    newMoveCount = parseInt(moveCount);
    newMoveCount = newMoveCount + 1; 
    $('span #moveCount').html('newMoveCount');
}

html

<div id="meta">
<h1>Checkers</h1>
<h4>Moves: <span id="moveCount">0</span></h4>
</div>
like image 341
Leahcim Avatar asked Sep 06 '12 19:09

Leahcim


1 Answers

You can remove the space if it's the span's id - space specifies descendants.. so you are actually looking for descendants of span with an id=moveCount

$('span#moveCount').html();

Or since you are selecting by ID you don't need to specify span as ID selector is the fastest - ID's are unique so it will always look for the first element with that id

$('#moveCount').html();
like image 199
wirey00 Avatar answered Oct 02 '22 09:10

wirey00