0

I'm trying to write code to make a text box appear and disappear based on whether or not a checkbox is checked, but while it works exactly the way I want it to in chrome, .onclick and .onchange seem to do nothing in firefox.

Trigger code:

document.getElementById('Other').onclick = ChangeOtherState;

'Other' html code:

<p>
  <label for="Other">Other</label> 
  <input type="checkbox" id="Other" class="Types[]" value="Other"/>
  <textarea id="OtherText" name="Other">Please enter other types here</textarea> 
</p>

State change function:

 function ChangeOtherState() { 

var otherCB = document.getElementById("Other");
var otherTB = document.getElementById('OtherText');

if (otherCB.checked) {
    otherTB.style.display='block';
    otherTB.removeAttribute('disabled');
} else {
    otherTB.style.display='none';
    otherTB.setAttribute('disabled','disabled');
};
}

Is there a way to make .onclick/onchange work? if not what can I use to get the same functionality without jQuery?

EDIT: For future readers, try to change the position of the trigger declaration to right after initialization.

Jayleaf
  • 31
  • 1
  • 9
  • 1
    Works [fine for me in FF dev](https://jsfiddle.net/oqnm7617/). – Andy May 01 '16 at 15:22
  • It seems to work if I change the position to be right on initialization, I'm not really sure why the rest of the code would prevent it from working, I guess that's as good a solution as any. – Jayleaf May 01 '16 at 15:42
  • The "trigger code" needs to happen at a point *after* the checkbox has actually been added to the DOM. In the jsfiddle, by default code is set up in a "load" handler, so it happens when the whole DOM is ready. If you put that code in a plain ` – Pointy May 01 '16 at 15:46

1 Answers1

0

Please find here others topics talking about the issue.

select onclick onchange not working onchange / onclick in a checkbox doesn't work in IE OnClick Dropdown works in FireFox but not Chrome or IE?

It seems to have workaround already discussed.

Regards,

Community
  • 1
  • 1
Thogerar
  • 339
  • 1
  • 7
  • There's no apparent problem in the first place. – Pointy May 01 '16 at 15:30
  • I'v checked all but one of those before I posted the question, but I can't add the onchange to the html element, it has to be in a separate file. – Jayleaf May 01 '16 at 15:32