views:

37

answers:

3

hi I am looking for a way to change the value what I passed to function once a button is pressed

<span id="showHidden" style=" margin-left:726px; position:relative; top:40;">
        <input type="image" id="btnShowHidden" src/images/hide.gif" onclick="showHiddenRecords(1);" />
        </span>  

As you see in showHiddenRecords, its passing a value of 1. What I want is to toggle these value every time a user clicks the image. so if its one it will change to zero and if its zero it will chnage back to one. Is this possible? how?

e.g; showHiddenRecords(1) showHiddenRecords(0)

+3  A: 

You can do it like this:

$(function() {
  $("#btnShowHidden").click(function() {
    this.value = this.value == "1" ? "0" : "1";
  });
});

Then you can remove the in-line functions all-together.

If you want to toggle something else, you can use .toggle(), like this:

$(function() {
  $("#btnShowHidden").toggle(function() {
    showHiddenRecords(1);
  }, function() {
    showHiddenRecords(0);
  });
});
Nick Craver
Nick, I think you need a ? where you have a :
Ken Redler
@Ken - woops, updated!
Nick Craver
I am confused..how will it pass the value to the function?
tried nothing happens
@user295189 - What is the function doing? The second version will run your literal function, but it would be easier if we understood what the function was doing.
Nick Craver
the function is just taking 1 or 0 and sending it to a php post for a query
@user295189 - Did you try the second version, removing the in-line `onclick`?
Nick Craver
+1  A: 

Remove onclick="showHiddenRecords(1);" from the input tag, then add in this jQuery in $(document).ready();:

$("#btnShowHidden").toggle(function(){showHiddenRecords(1);},function(){showHiddenRecords(0);});

This uses jQuery's toggle function to do things on alternate clicks

Adam
tried..after clicking nothing happens
@user295189 that's strange. you've got it inside $(document).ready(function(){ //put code here}); ? What browser are you using? what happens if you replace the showHiddenRecord functions with alerts?
Adam
+1  A: 

I assume you have some hidden records on page?

In jQuery you can use the toggle function to handle this...

<div></div>
<div class="hiddenrecord"></div>
<div></div>
<div></div>
<div class="hiddenrecord"></div>
<div class="hiddenrecord"></div>

<span id="showHidden" style=" margin-left:726px; position:relative; top:40;">
        <input type="image" id="btnShowHidden" src/images/hide.gif" onclick="$('.hiddenrecord').toggle();" />
</span>
Andir