1

I'm trying to loop through several objects within the same id, instead of generating a new id for each object and writing out the script, as it is shown below.

<script type="text/javascript">
  function get_object(id) {
   var object = null;
   if (document.layers) {
    object = document.layers[id];
   } else if (document.all) {
    object = document.all[id];
   } else if (document.getElementById) {
    object = document.getElementById(id);
   }
   return object;
  }
get_object("inputdata").innerHTML=DrawCode39Barcode(get_object("inputdata").innerHTML,1);
get_object("inputdata2").innerHTML=DrawCode39Barcode(get_object("inputdata2").innerHTML,1);
</script>
3
  • 5
    document.layers? document.all? Why are you writing code for Netscape 4 and Internet Explorer 5.0? Commented Sep 19, 2015 at 17:33
  • can you be more specific Commented Sep 19, 2015 at 17:40
  • Could you try to be a little more descriptive? Like what is your requirement? Do you have several HTML elements under one element and you want to iterate over all those inner elements? Commented Sep 21, 2015 at 4:39

2 Answers 2

1

querySelectorAll (with a CSS class selector as its argument) and getElementsByClassName will both return an array-like HTML collection of elements that are a member of a given class. You can use it with a simple for loop.

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

3 Comments

Im not sure how to implement this? I accidentally said class when intending id.
@user241619 — You cannot have multiple elements with the same id. The HTML specification forbids it and it defeats the whole purpose of using an id. If you want multiple elements with a shared identifying feature, use a class, that is what class is designed for.
Thank you for your help. What does this look like in terms of the above code. Im not entirely familiar with javascript.
0

For example looping all Input I use this code

$('input').each(function() {
    if(!$(this).val()){
        alert('Some fields are empty');
       return false;
    }
});

Comments

Your Answer

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