Solving a Javascript Issue

I am using a code like this:

<a href="javascript:void(0);" onclick="showall('#theid#');">Show Range</a>
<br>
<div class="abc">
<cfloop from="1" to="10" index="k">
<dv id="#theid#_#k#" style="display:none;">
xx.ss<br>
</div>
</cfloop>

Open in new window



now i am using a function like this javascript:

function showall(id) {
  try {
    var counter = 10;
    for(counter=1;counter<=10;counter++) {
      document.getElementById(id+'_'+counter).style.display='block';
    }
  }
  catch(exception) {}
}

Open in new window


so it is working fine.

The issue is if i do not use try catch, it wil throw an error, the cfloop has only 5 elements, so to ignore that i used try and catch.

what could be the best possible alternative way to use this kind of thing, i had to change it, i am not sure what should i do.

he elements withing loop can be 5,3,4. They are not static

so guide me

Thanks
LVL 16
Gurpreet Singh RandhawaCEOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
jQuery can help you in here.
You can search for elements which ID starts with the id argument you receive:
function showall(id) {
     $('[id^="' + id + '_"]').show();
};

Open in new window

I wrote this directly here but I think it's correct :)
0
Alexandre SimõesManager / Technology SpecialistCommented:
Otherwise you can also rework your code like:
function showall(id) {
    var counter = 10;
    for(counter=1;counter<=10;counter++) {
      val elem = document.getElementById(id+'_'+counter);
      if(elem){
        elem.style.display='block';
      }
    }
}

Open in new window

0
Alexandre SimõesManager / Technology SpecialistCommented:
So here's another academic version :)
http://jsfiddle.net/AlexCode/sdtg3n51/

Basically you just use the container id and loop through all its children nodes.
function showAll(id){
    var container = document.getElementById('container'),
        childElements = container.childNodes;
    
    for (var i = childElements.length - 1; i >= 0; i--) {
      var elem = childElements[i];
        if(elem.id && elem.id.indexOf(id + '_') === 0){
            elem.style.display='block';
        }
    };
}

Open in new window

Just choose the one you like best! :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.