-1

I have these blocks:

function generateEmail(){
if 
(document.getElementById('emailOpt1').checked = "true") {
document.getElementById('generatedEmail').innerHTML = emailOpt1.value
}
else if
(document.getElementById('emailOpt2').checked = "true") {
document.getElementById('generatedEmail').innerHTML = emailOpt2.value
}
else if
(document.getElementById('emailOpt3').checked = "true") {
document.getElementById('generatedEmail').innerHTML = emailOpt3.value
}
else if
(document.getElementById('emailOpt4').checked = "true") {
document.getElementById('generatedEmail').innerHTML = emailOpt4.value
}
} 

and this:

<div class="radioEmailType" id="emailClass">  
<input type="radio" id="emailOpt1" name=emailType value="email_c1">
 <label for="emailOpt1">class-one</label>
<input type="radio" id="emailOpt2" name=emailType value="email_c2">
 <label for="emailOpt2">class-two</label>
<input type="radio" id="emailOpt3" name=emailType value="email_c3">
 <label for="emailOpt3">class-three</label>
<input type="radio" id="emailOpt4" name=emailType value="email_c4">
 <label for="emailOpt4">class-four</label>
</div>
<button type="button" class="gButton" onclick=generateEmail()">GENERATE EMAIL</button>
<textarea id=generatedEmail></textarea></td></tr>

when I hit the 'generate email' button after selecting one of the 'radios', the code seems to revert the selection back to the first option as I keep on getting the first option on the textarea.

any ideas and a possibly a simpler way to do this will be appreciated.

note: I had to go this route since the user wants the radios to be buttons.

Vahid Farahmandian
  • 6,081
  • 7
  • 42
  • 62
  • 5
    Compare with == and use keywords like true/false – Andrew Li Jul 03 '16 at 14:51
  • When comparing things in a condition, use `==`. ex. `else if (document.getElementById('emailOpt4').checked == true) {` A Boolean doesn't use quotes, strings use quotes. true, false they are Boolean. – zer00ne Jul 03 '16 at 14:51
  • ~was changing a lot in the code and that was one of the things i checked. when i use == on the conditional it doesn't seem to output the value on the textarea, it only does it when its a single '=' – Ramizes Obias Jul 03 '16 at 14:54
  • ~changing to == and boolean without quotes... – Ramizes Obias Jul 03 '16 at 14:54
  • ~ugh! i fooled around for 30 mins, 5 mins to construct this question/post and 2 minutes to follow your instructions to get the problem fixed... thanks guys! – Ramizes Obias Jul 03 '16 at 14:57

1 Answers1

0

I fix it :

Change :

.checked = "true"

to :

.checked == true

Final code :

<html>
    <head>
    </head>
    <body>
     <div class="radioEmailType" id="emailClass">  
    <input type="radio" id="emailOpt1" name=emailType value="email_c1">
     <label for="emailOpt1">class-one</label>
    <input type="radio" id="emailOpt2" name=emailType value="email_c2">
     <label for="emailOpt2">class-two</label>
    <input type="radio" id="emailOpt3" name=emailType value="email_c3">
     <label for="emailOpt3">class-three</label>
    <input type="radio" id="emailOpt4" name=emailType value="email_c4">
     <label for="emailOpt4">class-four</label>
    </div>
    <button type="button" class="gButton" onclick="generateEmail()">GENERATE EMAIL</button>
    <textarea id=generatedEmail></textarea>
    <script>
        function generateEmail(){
            if (document.getElementById('emailOpt1').checked == true) {
            document.getElementById('generatedEmail').innerHTML = emailOpt1.value
            }
            else if (document.getElementById('emailOpt2').checked == true) {
            document.getElementById('generatedEmail').innerHTML = emailOpt2.value
            }
            else if (document.getElementById('emailOpt3').checked == true) {
            document.getElementById('generatedEmail').innerHTML = emailOpt3.value
            }
            else if (document.getElementById('emailOpt4').checked == true) {
            document.getElementById('generatedEmail').innerHTML = emailOpt4.value
            }
        }
    </script>
    </body>
</html>
Ehsan
  • 12,655
  • 3
  • 25
  • 44