0

I am trying to get this snip of code to work in a while loop instead of an if. I need each instant of num_# to not be displayed if the printLoad_# val is empty. So if printLoad_1 value = nothing, the Num_1 would not be displayed, and then the printLoad_2 would check to see if its num_2 is empty and so on. The problem I am having is the function stops before checking each section. Im not sure if a do-while will work.

$(document).ready(function() {
 if(document.getElementById("printLoad_1").value == "")
 {
 document.getElementById('num_1').style.display = 'none';
 }
 if(document.getElementById("printLoad_2").value == "")
 {
 document.getElementById('num_2').style.display = 'none';
 }
 if(document.getElementById("printLoad_3").value == "")
 {
 document.getElementById('num_3').style.display = 'none';
 }
 if(document.getElementById("printLoad_4").value == "")
 {
 document.getElementById('num_4').style.display = 'none';
 }
 if(document.getElementById("printLoad_5").value == "")
 {
 document.getElementById('num_5').style.display = 'none';
 }
});
asked Jul 1, 2013 at 23:43
7
  • Is your question "how do I use a while loop" or is it "what's wrong with my code"? Commented Jul 1, 2013 at 23:44
  • its a bit of both, sorry Commented Jul 1, 2013 at 23:45
  • well, do you get any errors? Commented Jul 1, 2013 at 23:46
  • Diagnosis: seems like a case of iditis. Use common classes, and target elements by index, it'll be much simpler. Commented Jul 1, 2013 at 23:46
  • iditis? More like inexperience. Thanks anyways... Commented Jul 1, 2013 at 23:48

5 Answers 5

4

Have you considered just compositing the strings instead of this verbose construction? Something like this:

for( var i=1; i<=5; i++ ) {
 if( document.getElementById('printLoad_'+i).value === '' ) {
 document.getElementById('num_'+i).style.display = 'none';
 }
}
answered Jul 1, 2013 at 23:47
Sign up to request clarification or add additional context in comments.

2 Comments

This actually works for the inputs, but I am having trouble getting this to work dynamically. I will use what you have given me and try to come up with another question that builds off this one. Thanks!
@user2089255 If by dynamic you mean a variable amount of inputs then look at my answer.
2

Assuming you're using jQuery and that your elements are in order, I'd forget about ID's. If you use common classes, say .printload and .num, then you can easily target elements by index like:

$('.printload').each(function(i){
 if (!this.value) $('.num').eq(i).hide();
});
answered Jul 1, 2013 at 23:54

2 Comments

@user2089255 note that printLoad and num is the class of the element.
Yes. When using jQuery, anytime you see the id="*_#" type constructs, you can almost always do this better using classes instead of (or in addition to) id's.
1

if you have a variable amount of printLoad and num:

var i = 1,
pl=document.getElementById('printLoad_'+i),
num = document.getElementById('num_'+i);
while(pl !== null && num !== null){
 if(pl.value === ""){
 num.style.display = 'none';
 }
 i++;
 pl=document.getElementById('printLoad_'+i),
 num = document.getElementById('num_'+i);
}
answered Jul 1, 2013 at 23:54

1 Comment

Well this works too, same problem I am having with other working script. I think I have my code in an incorrect place. Ill move things around and see.
1

Here is a slight modification of Ethan's answer that "works dynamically". I've also updated it to use jQuery. This could be handled cleaner if CSS classes and a hierarchy relationship were used, but that would affect how the DOM needed to be generated ..

for (var i = 1; /* break */; i++) {
 var printEl = $('#printLoad_' + i)
 if (printEl.length) {
 if (!printEl.val()) {
 $('#num_' + i).css({display: 'none'})
 }
 } else {
 // No #printLoad_N, guess we're done looking
 break
 }
}
answered Jul 1, 2013 at 23:56

Comments

0

Per @elclanr 's answer:

Use common classes, and target elements by index, it'll be much simpler.

Set your "printLoad" elements to class='printLoad' and your "num" elements to class='num'. Then...

for (i=0;i<document.getElementByClass('printLoad').length;i++)
{
 if (document.getElementByClass('printLoad')[i].value == "")
 {
 document.getElementByClass('num')[i].style.display='none';
 }
}
answered Jul 2, 2013 at 0:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.