Jump to content

Help needed with JavaScript toggle button


retro

Recommended Posts

Hi,

 

I am a complete newbie at JavaScript, so apologies for this more than likely easy-to-answer question!

 

I took a simple script that toggles a button's value based on whether an option box was enabled or disabled (the button also toggled this) and stripped the option box out.  Here is what I came up with:

 

<script type="text/javascript" language="javascript">
function toggle()
{
if (document.getElementById("btnToggle").value == "OFF")
{
document.getElementById("btnToggle").value="ON"
return
}
if (document.getElementById("btnToggle").value == "ON")
{
document.getElementById("btnToggle").value="OFF"
return
}
}
</script>

 

I then used a SWITCH statement to give $c a value of "OFF" or "ON" based on a field in a table.  I then made the button:

 

<input type="button" name="btnToggle" value="<?php echo $c ?>" onclick="toggle()" />

 

When the page is loaded, the button correctly displays ON or OFF depending on the field.  However, nothing happens when I click the button.  I am sure this is in the .value == "OFF" part. How can I remedy this?

 

Thanks in advance for any help offered!

Link to comment
Share on other sites

Oops!  I just noticed that I hadn't set the id in the HTML!  :-\  It works now - id="btnToggle", same as name.

 

However, when I use $b = $_POST['btnToggle'] after submitting the form, $b doesn't return anything.  How can I get around this?

 

*edited* think I'm half asleep today! 

Link to comment
Share on other sites

An input type=button doesn't send its value. I don't know how you are submitting your form, but I assume you are trying to tell which button has been toggled in the form? You will probably want to use a hidden input and add something to your toggle function like this:

document.getElementById("hiddenInput").value = document.getElementById("btnToggle").value;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.