Take the 2-minute tour ×

I have some jQuery code like this:

// Called right away after someone clicks on the vote up link
    var problem_id = $(this).attr("data-problem_id");

    var class_name = ".votes_"+problem_id;
    alert ("class name: " + class_name);

    var x = $(".votes_214").val();
    alert ("x: " + x);      // returns nothing at first, but correct values later.

    vote(problem_id , 1);

    //Return false to prevent page navigation
    return false;       

and here is the vote function:

// Global function
var vote = function(problem_id , vote) 
    var dataString = 'problem_id=' + problem_id + '&vote=' + vote;

    // The person is actually logged in so lets have him vote
                type: "POST",
                url: "/problems/vote.php",
                data: dataString,
                success: function(html)
                    alert("data: " + html);
                    var class_name = ".votes_"+problem_id;
                    alert ("class name: " + class_name);

                    var x = $(class_name).val();

                    alert ("curr val: " + x);
                error : function(html) 
                    errorMessage = html;

                    if ( html == "not_logged_in" )
                        queue.login = false;

                        //set the current problem id to the one within the dialog

                        // Try to create the popup that asks user to log in.
                        //  $dialog.dialog('open');

                        errorMessage = "";

                        return false;
                    if ( errorMessage == "already_voted" )
                        // Display a dialog box saying that the user already voted
                        $('<div />').html('You already voted this way on this problem.').dialog();
                    if (  errorMessage == "error_getting_vote" )

                        $('<div />').html('Error getting existing votes.').dialog();
                        // ? :)
                } // End of error case  
            }); // Closing AJAX call.
return false;

The AJAX call gets made and returns the correct values. The problem is that I can not get and set the value of the class votes_problem_id_value that I set to any real value of the problem id here in my PHP:

        echo '<span class="half_text" style="color: #B77F37;">'.$problem_date.'</span> <span id="votes" class="votes_'.$problem_id.' half_text" style="padding-left: 10px;">'.$vote.'</span><strong> <a class="vote_up" style="font-size: 80.0%; color: #295B7B; font-weight:bold; text-decoration:none;" href="#" data-problem_id="'.$problem_id.'">Important</a></strong> | <strong><a class="vote_down" style="font-size: 80.0%; color: #295B7B; font-weight:bold; text-decoration:none;" href="#" data-problem_id="'.$problem_id.'">Not Important</a></strong>';

Any idea what I am doing wrong and how come I can not get the value or set the contents of that span class="votes_'.$problem_id.' half_text"


share|improve this question
is $(class_name) an array? does $(class_name)[0].val(html); work? –  sissonb Nov 8 '11 at 23:48
@sissonb - $(class_name) is a jQuery object, not an Array. $(class_name)[0] is either an element or undefined, neither of which have a .val() method. So, no, that could not work. –  gilly3 Nov 9 '11 at 0:37
add comment

2 Answers

up vote 2 down vote accepted

.val() is for form elements, not spans. Use .text() (or maybe .html()) to get the text contained in a span.

var x = $(class_name).text();

Or, to set a custom attribute on a span, use .attr():

var x = $(class_name).attr("value");
$(class_name).attr("value", html);

Or, to associate custom data with a span, use .data():

var x = $(class_name).data("value");
$(class_name).data("value", html);
share|improve this answer
Just tried to do it with .text() and still didn't work. Anything else that could be going wrong? –  GeekedOut Nov 8 '11 at 23:48
Actually just got it to work!! Will accept in 2 min. –  GeekedOut Nov 8 '11 at 23:50
add comment

I see that your PHP is treating POST values like they are globals. If your register_globals setting is off, $vote and $problem_id will be unset, unless you explicitely set them. The best practice is to leave register_globals off, and use the superglobal $_POST instead.

So, try class="votes_'.$_POST['problem_id'].' half_text" instead.

Here's the PHP doc on register_globals:

share|improve this answer
How did you conclude there is any issue with register_globals? OP showed only part of the PHP code, responsible for generating part of the HTML. I give -1 –  Tadeck Nov 8 '11 at 23:57
add comment

Your Answer


