Hello everyone,
I'm trying to select same "id" for more than one input. My code works with 1 input, but when I add 2 textareas or more my code stops working...

Here's my code:

Javascript

   *<script> 
function validateComment() {




var value = $("[id=commentbox]").val(); 

var word = value.toLowerCase();


    // make sure this word is lowercase
if( document.myForma.commentbox.value == "" )
   { document.getElementById('comment-error').innerHTML = '<style> #error-comment-info_empty {display:block !important; }</style>';  return false;  }

 if (word.indexOf("bad-word") != -1) {
       document.getElementById('comment-error').innerHTML = '<style> #error-comment-info {display:block !important; }</style>';    return false;} 
 if (word.indexOf("bad-word") != -1) {
       document.getElementById('comment-error').innerHTML = '<style> #error-comment-info {display:block !important; }</style>';   return false;} 

       if (word.indexOf("bad-word") != -1) {
       document.getElementById('comment-error').innerHTML = '<style> #error-comment-info {display:block !important; }</style>'; return false;} 

       if (word.indexOf("bad-word") != -1) {
       document.getElementById('comment-error').innerHTML = '<style> #error-comment-info {display:block !important; }</style>';   return false;}

 else { 


  return false;
  }

}
</script>*





  <textarea id="commentbox" class="auto" rows="2" data-min-rows="2" name="commentbox" placeholder="Write your staff here ..."></textarea>  

   <textarea id="commentbox" class="auto" rows="2" data-min-rows="2" name="commentbox" placeholder="Write your staff here ..."></textarea>

Thanks :)

Ids should be unique. use class instead

Every HTML element on the page should have its own unique id.

that's not the solution I'm looking for... I need to **select all with the same id or class... **

Could you please give an example of using class
??

<div class="wibble">1</div>
<div class="wibble">2</div>
<div class="wibble">3</div>

and javasript would be like this ? Please correct me

<script> 
function validateComment() {
var value = $("[class=commentbox]").val(); 
var word = value.toLowerCase();
    // make sure this word is lowercase
if( document.myForma.commentbox.value == "" )
   { document.getElementsByClassName('comment-error').innerHTML = '<style> #error-comment-info_empty {display:block !important; }</style>';  return false;  }
 if (word.indexOf("bad-word") != -1) {
       document.getElementsByClassName('comment-error').innerHTML = '<style> #error-comment-info {display:block !important; }</style>';    return false;} 
 if (word.indexOf("bad-word") != -1) {
       document.getElementsByClassName('comment-error').innerHTML = '<style> #error-comment-info {display:block !important; }</style>';   return false;} 
       if (word.indexOf("bad-word") != -1) {
       document.getElementsByClassName('comment-error').innerHTML = '<style> #error-comment-info {display:block !important; }</style>'; return false;} 
       if (word.indexOf("bad-word") != -1) {
       document.getElementsByClassName('comment-error').innerHTML = '<style> #error-comment-info {display:block !important; }</style>';   return false;}
 else { 
  return false;
  }
}
</script>

Is

getElementsByClassName

right? OR should I use

getElementByClassName

I need to **select all with the same id or class... **

again... each element needs its own unique ID. Target the elements by class if you are not going to have a unique id for each element as suggested by DaveAmour.

There is no getElementByClassName. its getElementsByClassName. When you get the elements by className it places them in an array. just access each via index, or loop through the array to access each element.

For example..

<script>
function myfunc() {
    var x = document.getElementsByClassName("comment-error");
    x[0].innerHTML = "Error-0";
    x[1].innerHTML = "Error-1";
}
</script>
Member Avatar
diafol

Are you mixing jQuery and vanilla js? Not that there's any crime against it, but if you're loading the library, you may as well use it. Just wundrin...

@Sofersurfa
You should never, ever, assign the same ID to two or more subjects within the same popullation. This act in direct violation with the very definition of ID, its core purpose and meaning.