SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamically modify the value of a button

    Hello,
    I have a button but I would like its value to change depending on what the user is doing. How can I do it?
    Suppose I have a function "getValue()" that would return a string. How can I make this string the value of my button?
    I would be something like that but It does not work

    Code:
    <td id="its_button" style="display:none;">
    <input type="button" onClick="reDirect(1)" value=getValue() ></td>
    Thank you for your help

  2. #2
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function getValue(){
    newValue='forty two'
    return newValue;
    }
    
    <td id="its_button" style="display:none;"
    <input type="button" onClick="reDirect(1);this.value=getValue()" value="click here" ></td>

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think this is what I need...
    In your code, when I click on the add button, I change its value.... now this is not what I want to do.
    When I click on another button, I make two fields visible. One of this field is the button ... and I want it with a specific value (it will change depending on the button I click)

  4. #4
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You will have to change value ine the onclick of the other buttons...
    Code:
    <input type="button" onClick="document.getElementById('mybutton').value=this.value" value="action 1" ><br/>
    <input type="button" onClick="document.getElementById('mybutton').value=this.value" value="action 2" ><br/>
    <input type="button" onClick="document.getElementById('mybutton').value=this.value" value="action 3" ><br/>
    <input type="button" onClick="document.getElementById('mybutton').value=this.value" value="action 4" ><br/>
    <input type="button" onClick="document.getElementById('mybutton').value=this.value" value="action 5" ><br/>
    <input type="button" onClick="document.getElementById('mybutton').value=this.value" value="action 6" ><br/>
    <input type="button" onClick="document.getElementById('mybutton').value=this.value" value="action 7" ><br/>
    <br/><br/>
    <input type="button" id="mybutton" onClick="alert(this.value)" value="click here" >

  5. #5
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or
    Code:
    <script type='text/javascript'>
    
    function getValue(sel){
    document.getElementById('mybutton').value=sel;
    
    }
    </script>
    </head>
    
    <body>
    
    <table>
    
    <input type="button" onClick="getValue(this.value)" value="action 1" ><br/>
    <input type="button" onClick="getValue(this.value)" value="action 2" ><br/>
    <input type="button" onClick="getValue(this.value)" value="action 3" ><br/>
    <input type="button" onClick="getValue(this.value)" value="action 4" ><br/>
    <input type="button" onClick="getValue(this.value)" value="action 5" ><br/>
    <input type="button" onClick="getValue(this.value)" value="action 6" ><br/>
    <input type="button" onClick="getValue(this.value)" value="action 7" ><br/>
    <input type="button" onClick="getValue(this.value)" value="action 8" ><br/>
    
    
    <br/><br/>
    <input type="button" id="mybutton" onClick="alert(this.value)" value="value" />
    </body>

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THank you very much for your help!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •