6 Replies - 380 Views - Last Post: 09 June 2019 - 09:41 PM

#1 nero6014   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-March 19

javascript does not execute all if statements

Posted 17 March 2019 - 12:38 PM

The following code seems to stop after the first if statement proves true. I would like it to evaluate each if and execute statements for each one if all are true. Also the background color lines do not execute until I click on the form field, I wanted them to color the background on document ready. Thanks

<script> 
$(document).ready(function($){ 
var A = $( "#Q620_B_8" ).val(); 
var B = $( "#Q620_B_13" ).val(); 
var C = $( "#Q620_B_17" ).val(); 
var D = $( "#Q620_B_18" ).val(); 
var E = $( "#Q620_B_19" ).val(); 
var F = $( "#Q620_B_20" ).val(); 
var G = $( "#Q620_B_21" ).val(); 
var H = $( "#Q620_B_22" ).val();
   
if (typeof(A) != "undefined") 
{ 
$( "#Q620_B_8" ).attr( "disabled", true ); 
$( "#Q620_B_8" ).style.backgroundColor  = "gray";
}
if (typeof(B)/> != "undefined") 
{ 
$( "#Q620_B_13" ).attr( "disabled", true );
$( "#Q620_B_13" ).style.backgroundColor = "gray";
} 
if (typeof(C) != "undefined") 
{ 
$( "#Q620_B_17" ).attr( "disabled", true );
$( "#Q620_B_17" ).style.backgroundColor = "gray";
} 
if (typeof(D) != "undefined") 
{ 
$( "#Q620_B_18" ).attr( "disabled", true );
$( "#Q620_B_18" ).style.backgroundColor = "gray";
} 
if (typeof(E) != "undefined") 
{ 
$( "#Q620_B_19" ).attr( "disabled", true );
$( "#Q620_B_19" ).style.backgroundColor = "gray";
} 
if (typeof(F) != "undefined") 
{ 
$( "#Q620_B_20" ).attr( "disabled", true );
$( "#Q620_B_20" ).style.backgroundColor = "gray";
} 
if (typeof(G) != "undefined") 
{ 
$( "#Q620_B_21" ).attr( "disabled", true );
$( "#Q620_B_21" ).style.backgroundColor = "gray";
} 
if (typeof(H) != "undefined") 
{ 
$( "#Q620_B_22" ).attr( "disabled", true );
$( "#Q620_B_22" ).style.backgroundColor = "gray";
}  
}); 
</script>


Is This A Good Question/Topic? 0
  • +

Replies To: javascript does not execute all if statements

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,095
  • Joined: 12-December 12

Re: javascript does not execute all if statements

Posted 17 March 2019 - 02:26 PM

Please describe in what way it "seems to stop". Are there errors in your browser's console?

You should also indent your code.
Was This Post Helpful? 0
  • +
  • -

#3 nero6014   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-March 19

Re: javascript does not execute all if statements

Posted 17 March 2019 - 02:58 PM

Thanks for looking at it, what I meant by stopping was that it would disable the first control but do nothing to the rest. I did look in the console and it is the .style.backgroundColor = "gray" lines that are causing the problem. When I commented all those out it did disable the proper controls. Any ideas on how to get the background color to gray out? Also, I am fairly new to javascript, if you could give me an example of proper indenting for Javascript, maybe just a few lines of my code, I will copy your example and learn from it. Thanks again for your time.
Was This Post Helpful? 0
  • +
  • -

#4 nero6014   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-March 19

Re: javascript does not execute all if statements

Posted 17 March 2019 - 03:23 PM

I played with it some more and came up with this, the only problem is the fields won't turn gray until I click on them. is there a way to make them be gray on document ready or should I be using something else besides document ready?

$( "#Q620_B_8" ).attr( "disabled", true ).css("background-color","gray");

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,095
  • Joined: 12-December 12

Re: javascript does not execute all if statements

Posted 17 March 2019 - 03:31 PM

style.backgroundColor is a setting that applies to a (Javascript) DOM object, $() yields a jQuery object. You would use [0] to convert jQuery to a DOM object.

Which tutorial are you studying? Any decent tutorial will teach you early on about indenting code.

You could also much simplify your repetitive code using a loop.
Was This Post Helpful? 0
  • +
  • -

#6 nero6014   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-March 19

Re: javascript does not execute all if statements

Posted 17 March 2019 - 03:54 PM

Ok, thanks
Was This Post Helpful? 0
  • +
  • -

#7 sagar.chavada   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-April 19

Re: javascript does not execute all if statements

Posted 09 June 2019 - 09:41 PM

My point of view you should go through if and if else statement or using for loop.May be this technique try to solve your issue.best example of If else and for loop is pyramid of Doom.this one is the best example of looping.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1