1

I have a while loop in my PHP page where I look the following...

    print $divLeft.strip_tags($row->twitterUser)."?size=normal\"/><br \/>".$row->twitterUser.$divRight."<a href='javascript:void(0);' id='vote_$row->id' class='getPoint'>Get " .$row->coff. "<input type='hidden' value='$row->coff' id='credoff'/> credit(s)</a><br /></div>$clearDiv</div></div>"; 

I have a value set in my hidden field which I then call in my javascript...

{        
   var theid = $(this).attr("id");
   var onlyID = theid.split("_");
   var onlyID = onlyID[1];
   credoff = $("#credoff").val();


    $.ajax({            
      url: 'do.php',
      type: 'POST',          
      data: "userID=" + onlyID,
      success: function(data) {
          if(data != "success1" && data != "success5") {
               $("#" + theid).text(data);  
          }else{

              $("#thediv_" + onlyID).fadeOut("slow");
              $('#creditsBalance').fadeOut("slow");
              newbalance = parseInt($('#creditsBalance').text());

          if(data != "success5") { 
              alert('Credits offered = '+credoff);

The only thing is in my javascript its grabbing the highest 'credoff' variable value on the page, not the one clicked on, does this make sense?

4
  • There is an indentation problem or you missed the } that closes the else Commented May 24, 2011 at 22:10
  • I've changed the loop input from id to class and tried 'credoff = $(this).children('input.credoff:hidden').val()' which now returns undefined, I've also tried 'var credOff = $(this).find('input').val();' which breaks my JS and stops the rest of the code like alert and fade in execute... Commented May 24, 2011 at 22:19
  • what is in 'this'? How is the function getting called? Commented May 24, 2011 at 23:55
  • Perhaps you made a typo in my function. Here's a base fiddle showing the base selectors working: jsfiddle.net/ehNKJ Commented May 25, 2011 at 0:01

2 Answers 2

0

First off, $('#credoff') is an id... and IDs are unique to a page. Secondly, if there were more than one (and built properly through a CSS selector), the way you're calling $('#credoff') would only give you the value of the first one, since you're not associating it with the event target.

Since it looks structurally like the input is a child of the (which is odd, too, btw), you'll need to use a selector like this to get credOff:

$('.getPoint').click(function(){
    // properly associated with the event target.
    var credOff = $(this).find('input').val();
    // etc.
}
0

Element IDs must be unique throughout the page; if they are not, then attempting to select by ID is undefined. Your best bet is to use something like this:

<input type="hidden" ... class="credoff" />

And then in the javascript something like this:

credoff = $(this).children('input.credoff:hidden').val()
1
  • This is returning Undefined, Is it possible I can give the input a name and use var credoff = getelementbyname(credoff).value; ? Commented May 24, 2011 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.