Skip to content
Advertisement

Radio buttons checked changing submit text

My site structure consists on an index.php which is styled by a css file. It then includes the following php code in a separate file:

JavaScript

It appears on screen with no problems in the correct layout as my css style sheet. What I would like this to do is when I select the “Subscribe” radio button the submit button text “OK” changes to “Join”. When I click on the Unsubscribe button text “OK” or “Join” changes to “Leave”.

I tried to make some code from research:

JavaScript

I think this kind of worked in that it changed to Join (replacing the OK line, but obviously didn’t update on clicking unsubscribe. I guess it would update on refreshing the page if my default wasn’t join. I guess I need to do some form of onclick but then I have no idea how to adjust that span ok bit.

Please help? Many thanks Chris

Advertisement

Answer

Here is a solution in plain JavaScript without jQuery. It avoids the unnecessary overhead.

This should work, but I haven’t had a chance to test it:

JavaScript

However, you should not do it like this.

You should combine the two radio buttons into a radio group.

In that case you will listen for radio group to change, get the value of the radio group, set button text according to the value.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement