Link to home
Start Free TrialLog in
Avatar of leawasson
leawasson

asked on

Change checkbox text onchange with javascript

I have the following checkbox:
<c:set var="value" value="${form.flagNpOrSp}"/>
<input type="checkbox" name="flagNpOrSp" value="on" <c:if test="${value}">checked="checked" </c:if>  id="searchFlagNpOrSp"/>
<c:set id="searchFlagNpOrSpCaption" var="value" value="${form.flagNpOrSpCaption}"/>
<c:out value='${value}' />

How can I change the  the label of the checkbox based on what a user select in a dropdown?
Avatar of bugs021997
bugs021997
Flag of India image

Call this javascript on the
<script language="javascript">

function changeValue(){

        eval("document.myForm.flagNpOrSp.checked = false");
}

</script>



To change the label of the checkbox based on the dropdown selection use....

Here goes an example for you...

<html>
<head>
</head>
<body>
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td>
<p id="example3">I want to replace this text: <span id="ex3Span">right here </span>.</p>

<script type="text/javascript">

/*! sent 'this.selectedIndex' as parameter in call */
function replaceSpan(lvl)  
{
  // An array is easier to edit.
  // Here it's defined and used at the same time.
  var text =  
  [
    "Sentence relating to option 1",
    "Sentence relating to option 2",
    "Sentence relating to option 3"/**/  
  ][lvl-1];

var span = document.getElementById("ex3Span");
while(span.childNodes[0]) /* removing nodes */
span.removeChild(span.childNodes[0]);
span.appendChild(document.createTextNode(text));
}
</script>

<SELECT NAME = "option3" id="option3" onChange="replaceSpan(this.options[this.selectedIndex].value);">
<OPTIon VALUE = "1">Option 1
<OPTIon VALUE = "2">Option 2
<OPTIon VALUE = "3">Option 3
<OPTIon VALUE = "4">Option 4
</SELECT>
</td>
</tr>
</table>
</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bugs, please enlighten us and explain what this code is doing?

        eval("document.myForm.flagNpOrSp.checked = false");

???

and what is the [lvl-1]; doin in the following?

 var text =  
  [
    "Sentence relating to option 1",
    "Sentence relating to option 2",
    "Sentence relating to option 3"/**/  
  ][lvl-1];

and lastly why remove and append when you can change the innerHTML of the span???
@mplungjan

I was trying to give an example of how to go about it and not exactly working example to do the code. It would just enlighten the coder to enhance it.

Cheers
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@mplungjan

And the second section got accidentally placed here....i could not remove it....i apologies...
Avatar of leawasson
leawasson

ASKER

This was exactly what I was looking for.  Thank you very much!
Great...
It was a variation of b0lsc0tt's code so perhaps he deserved a split?

Michel
Thanks Michel (not just for the last post). ;-)

Leawasson, no worries about the split.  If you have a question about what a split is though let us know.

bol
That is true.  So sorry b0lsc0tt.  I am new to the site and only realized later that you could accept multiple answers.  Unfortunately it seems you can't do anything after the fact!  *Michelle mentally deducts points from Michel's next accepted answer*  :)   Thanks to everyone for their help.  
I'm glad you know now.  That  is most important.

For future info you can have a question reopened or get help changing what you did to close it.  You just post a message in the General Community Support zone (https://www.experts-exchange.com/Community_Support/General/).  You can even save this page (https://www.experts-exchange.com/Community_Support/General/newQuestionWizard.jsp) to automatically be taken to the question wizard for that zone.

No points are needed for posting.  The moderators and admins respond to posts and are VERY helpful.  Make sure you include the URL for the question in the message body and provide the details of what you need.  You don't have to use it for this question but this is good info to have.  Let me know if you have a question.

bol
Thanks for taking the extra time and effort to split this.  It was a fun question.  Thanks for the grade and the points.

bol