0

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 :)

Edited by SoferSurfa

5
Contributors
9
Replies
24
Views
2 Years
Discussion Span
Last Post by Troy III
0

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

0

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

Edited by SoferSurfa

0

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>

Edited by JorgeM

0

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...

1

@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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.