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!

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! 

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.