1

I am working in wordpress and I have created a custom plugin.In which I have get multiple data from the database and my code is like this.

<?php 
foreach($result as $res)
{    
?>
<input type="hidden" class="status" value="<?php echo $res->review_status; ?>" />
<button class="aprove" value="<?php echo $res->review_id; ?>">Aprove</button>
<?php 
}
?>

Now, I want to get hidden field value in jQuery. My jQuery code is like this:

 jQuery(".aprove").click(function(){
     var status = jQuery('.status').val();
     alert(status);
     });

When I click on button then it shows only first value of hidden field. For instance, the fist hidden value is 1 and second value is 0 then it display only fist value 1 for both button. So what shold I have to do to get different hidden value?

1
  • you have to create a jquery array and puss all hidden value in them and then do what ever stuff you want to do. Commented May 1, 2015 at 11:05

4 Answers 4

5

Try :

jQuery(".aprove").click(function(){
    jQuery('.status').each(function(){
        var status = jQuery(this).val();
        alert(status);
    });
});

.each will loop through all the classes and it will give alert every value of it.

JS Fiddel Demo

Updated

Updated Demo

Sign up to request clarification or add additional context in comments.

1 Comment

I want only one value when i click on button.
1

Here is your answer. for each button the value taken would be from the input element before the button element

 jQuery(".aprove").click(function(){
    var status = jQuery(this).prev('.status').val();
    alert(status);
 });

3 Comments

It works in fiddle but i dont know why i got an error in my localhost .
have u included the line.<script src="ajax.googleapis.com/ajax/libs/jquery/1.11.2/…>
It was my mistake. I had put hidden field in another <td> not in <td> of <button>. Therefor, it gave "undefine" error. Now it works. Thank you so much.
1
 var status = jQuery('.status').val();
 alert(status);

this will get the value of element first found on page and will return the result,

if you want all the input values use

jquery each() - https://api.jquery.com/jquery.each/

jQuery('.status').each(function(){
   alert($(this).val());
 });

// this will give you all the values you want, one by one

Comments

0

you can use .map like this to get what you want:

   $(document).ready(function(){
    	  var status=[]
    	  jQuery(".aprove").click(function(){
    		  status = $(".status").map(function() {
    			   return $(this).val();
    			}).get();
    			alert(status);//array of values
    		     });
      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <input type="hidden" class="status" value="0" />
	    <input type="hidden" class="status" value="1" />
	  <button class="aprove" value="">Aprove</button>

Comments

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.