Robbie Coyne Robbie Coyne - 1 year ago 63
HTML Question

How to change paragraph text in Javascript? (using values from radio buttons).(onclick event possibly not firing)

Basically, on my website I am attempting to have the option to choose between English and Spanish. How this would work is there would be two radio buttons, one for each language, and a button. When the button is pressed, the text of the paragraphs should be changed depending on the radiobutton that is checked. I have code which in my opinion should be valid but yet doesn't work.



<script type="javascript">
function ChangeLanguage()
document.getElementById("p1").innerHTML = "OF COURSE!";
document.getElementById("p1".innerHTML = "CLARO!";
catch (err)
return false;




<form name="Radio">



<input id="rbEnglish" name="rbLanguage" type="radio" checked="checked" value="English"/>

<label for="rbEnglish">English</label>

<input id="rbSpanish" name="rbLanguage" type="radio" value="Spanish"/>

<label for="rbSpanish">Spanish</label>

<input id="btnSubmit" type="submit" value="Submit" onclick="return ChangeLanguage();"/>




<p id="p1">



After some tampering it seems to me that the "onclick" event isn't working for some reason but I can't tell why. Please help either by correcting this code or offering alternative code.

Answer Source

Is it because you are missing a ')'

Change this: document.getElementById("p1".innerHTML = "CLARO!";

To this: document.getElementById("p1").innerHTML = "CLARO!";

See fiddle example: