2

i need a javascript function to be called on certain button click, upon that click the button name will change. ie,

<input type=button id = button1 value=boy onclick=function(boy,girl)>

i need the javascript function to take 2 parameters and check if the value of the button is the first paramter, then the value will become the 2nd and vice versa.

so if i press the button and it says BOY, it will become Girl and if i press the button and it says Girl it will become boy! using javascript please thx.

5 Answers 5

3
var foo = function(a,b,c){
  if(a.value==b){
    a.value=c;
  }
  else{
    a.value=b;
  }
};

and for the button

<input type="button" id="button1" value="boy" onclick="foo(this,"boy","girl")">
3

see here: http://jsfiddle.net/w9ed8/

or:

<input type=button id = button1 value=boy onclick="changeMe(this)">

<script>
function changeMe(obj){
  if(obj.value == "boy"){
     obj.value = "girl"
   }else{
     obj.value = "boy"
   }
}
</script>

Or

<input type=button id = button1 value=boy onclick="changeMe(this, 'boy' , 'girl')">

<script>
function changeMe(obj , param1 , param2){
  if(obj.value == param1 ){
     obj.value = param2
   }else{
     obj.value = param1 
   }
}
</script>
9
  • only working for boy and girl and the function is not working as asked :) he asked for a function which will compute at least 2 parameters :)
    – GorillaMoe
    Sep 21, 2011 at 10:41
  • Well I think ahead and I know y he asked for 2 params, but it is not necessary... (-:
    – fatnjazzy
    Sep 21, 2011 at 10:45
  • so he can use this function only on a button that has the value boy or girl, because it is not "neccessary" ;) if someone don't mind to write unreusable code this is pretty fine I guess ;)
    – GorillaMoe
    Sep 21, 2011 at 10:48
  • @Pranav you can take my code and use it on any button created and just edit the function call on the button itself and it will work.. fatnjazzy's code will only work for buttons with the value boy or girl, so fatnjazzy's solution is not reusable for buttons with a value "male" or "female", "single" or "married".. and so on
    – GorillaMoe
    Sep 21, 2011 at 10:49
  • 1
    @walialu:-ya right.. i think yours code is more generic in nature. +1 for you.
    – Pranav
    Sep 21, 2011 at 10:52
2

There are many ways to do this, but by getById function showed below you will have a portable function to access to DOM elements by id :

<html>
    <script type="text/javascript">

        function getById(a) {
           if (document.getElementById && document.getElementById(a)) {
              return document.getElementById(a)
           } else {
              if (document.all && document.all(a)) {
                 return document.all(a)
              } else {
                 if (document.layers && document.layers[a]) {
                    return document.layers[a]
                 } else {
                    return false
                 }
              }
           }
        }
        function myfunc()
        {
        getById("button1").value =(getById("button1").value=="boy")?"girl":"boy";
        }
    </script>
<head>
</head>
<input type="button" value="boy" id="button1" onclick="myfunc();" />
</html>
2
<input type=button id=button1 value=boy onclick='test(this,boy,girl)'>
<script>
      function test(Sender,boy,girl){
        Sender.value = Sender.value == boy ? girl : boy;
      }
</script>
1
function change()
{
 if((document.getElementById("button1").value)=="Boy")
 {
  document.getElementById("button1").value="Girl"
 }
 else
 {
  document.getElementById("button1").value="Boy"
 }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.