1

I have a button (input type=button)that toggles through 3 states using a bit of simple bit of javascript, this works well but I find that when I submit the form I cant pass the current value of the button.

<input type=button ID="1" value="$decMake" name=decMake onclick="toggle(this);">

The JS:

function toggle(button) {
  if(document.getElementById("1").value==""){
    document.getElementById("1").value="Strength";
  }
  else if(document.getElementById("1").value=="Strength"){
    document.getElementById("1").value="Opportunity";
  }
  else if(document.getElementById("1").value=="Opportunity"){
    document.getElementById("1").value="";
  }
}

I'm trying to do this with a simple input "hidden" statement but I think this is not enough and needs to be a bit cleverer then that, whats the best way of doing this

Thanks

8
  • 4
    valid HTML IDs may not start with numbers. Commented Feb 19, 2013 at 20:39
  • 3
    @jbabey I thought they can in HTML5... Commented Feb 19, 2013 at 20:41
  • 2
    @jbabey, as per the HTML5 spec: "The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.". IDs are no longer restricted from starting with a number, browsers have always supported them that way because developers are unreliable. Commented Feb 19, 2013 at 20:44
  • 3
    @jbabey That's not my point. You claimed they aren't valid. They are in HTML5. That's all I'm saying. Not arguing whether it's right to do this or not, but that's not what your comment was about Commented Feb 19, 2013 at 20:46
  • 1
    @Jimmmy Again, not my point. The comment had nothing to do with being "right", it had to do with validity. HTML5 supports it, so you can't just claim it's wrong... Commented Feb 19, 2013 at 20:48

1 Answer 1

4

A hidden input is probably the best way to do this.

<input type="hidden" id="buttonValue" value=""/>
<input type=button ID="buttonToggle" value="$decMake" name="buttonToggle" onclick="toggle(this);">

Also change the button id to something more descriptive.

function toggle(button) {
    var value=button.value;
    switch(value){
        case "": value="Strength"; break;
        case "Strength": value="Opportunity"; break;
        default : value=""; 
    }
    button.value = value;
    document.getElementById("buttonValue").value = value;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Nice... I was justing looking if someone had done this already
Hi Michael, thanks for that. normally I use an $input{'var_name'};
I'm not following. PHP is not my language of choice. However you want to set up the ids is fine. You can modify this JS to fit, or I can if you elaborate.
Thanks for that. normally I use an $input{'var_name'}; to get the value back into the form, is this the case here? Sorry I'm more html than javascript
Basically, all this does is copy the button value into a hidden input value. So when you post, you should be able to get it from the $input{'buttonValue'} I would imagine. But like I said, php isn't my strong suit. The html should be easy to follow though.
|

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.