2

I've made oninput for div with a class. I can't add an ID to the div. So here is my script:

var objecto = document.getElementsByClassName(" nicEdit-main ")[0];
objecto.oninput = function() {
    alert("Ok!");
}  

Here is my HTML:

<div class=" nicEdit-main   " style="width: 499px; margin: 4px; min-height: 149px; overflow: hidden;" contenteditable="true">1123215545<br></div>

Why does oninput not work for this div?

3
  • Is there any error in your browser console Commented Aug 28, 2015 at 6:30
  • jsfiddle.net/arunpjohny/r84rtmx5/1 - make sure you are running the script after the target element is loaded in the dom Commented Aug 28, 2015 at 6:31
  • thanx for this lesson. Write into answers. THanxc for help @ArunPJohny Commented Aug 28, 2015 at 6:33

1 Answer 1

4

It should just work fine, make sure your script is executed after the target element is loaded in the dom.

1 way is to use the dom ready/window load event handler

window.addEventListener('load', function() {
  var objecto = document.getElementsByClassName(" nicEdit-main ")[0];
  objecto.oninput = function() {
    alert("Ok!");
  }
})
<div class=" nicEdit-main   " style="width: 499px; margin: 4px; min-height: 149px; overflow: hidden;" contenteditable="true">1123215545
  <br>
</div>


Another is to place the script after the element like

<div class=" nicEdit-main   " style="width: 499px; margin: 4px; min-height: 149px; overflow: hidden;" contenteditable="true">1123215545
  <br>
</div>
<script>
  var objecto = document.getElementsByClassName(" nicEdit-main ")[0];
  objecto.oninput = function() {
    alert("Ok!");
  }
</script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.