I don't know javascript very well...

I have a form with the name register_group and a radio button for pc_parade_float with yes/no options. I want to display a different comment depending on if they clicked yes or no in the radio button. The code I have is as follows:

<script type='text/javascript'>
{document.writeln('use PayPal to pay your &#36;75.00 registration fee.')}
{document.writeln('use PayPal to pay your &#36;50.00 registration fee.')}

Note that this code is outside the <form></form> if that matters.

If I change the value of the button it does not change the value of the output. What am I missing. Do I need an on-click event in the <input> of the buttons to trigger the change? If so what would it be?


That code, as present above, runs when the browser reads/renders the HTML. It won't do what you expect.

First, you need to mentally separate your web server generating (maybe dynamic) HTML, your browser initially rendering the received HTML, and javascript modifying the rendered HTML later. They are three distinct things in three distinct environments (scopes). Once the HTML is generated and sent, the server no longer has any part in the process; in fact, the server's context vanishes. Once the browser finishes rendering the HTML, the document is 'closed'; nothing more can be written to (appended to) it. At this point, you may only use JavaScript to add, delete and change contents of the DOM (elements of the display). That means that the tags in your initial HTML must contain sufficient ID and/or name elements so that you can locate the elements you want to change. Look up "getElementById()".

It took me quite some time to consistently separate the three environs in my head and keep them separated.

Second, you need a predefined comment field with an ID or a name and perhaps containing something like "Membership included ($25)".

Third, you need an 'onchange' item in your radiobutton definition that will call a function that changes the innerHTML of the comment item above.

Fourth, you need to rewrite your JS as a function that the above 'onchange' element calls when the radiobutton changes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.