This topic is locked
[SOLVED]

 Nesting IF(s) in Javascript

11/7/2011 9:11:30 AM
PHPRunner General questions
W
wildwally author

Is it possible to nest ifs into Javascript. I can make this section of Hide/Show rows work.



//Set up my control variables

// the dropdown control

var ctrlDrawings = Runner.getControl(pageid, 'Drawings');

var ctrlReqDet = Runner.getControl(pageid, 'AttachQuestion');

//the target row to appear / disappear

var SHrow1 = document.getElementById("attachyes");

var SHrow2 = document.getElementById("answerno");

var SHrow3 = document.getElementById("answerno1");

var SHrow4 = document.getElementById("answerno2");

var SHrow5 = document.getElementById("answeryes");
//My default value on page load

SHrow1.style.display = 'none';

SHrow2.style.display = 'none';

SHrow3.style.display = 'none';

SHrow4.style.display = 'none';

//SHrow5.style.display = 'none';
//Attaching documents to request - if yes show attachemnt field.

ctrlDrawings.on('change', function(e){

if (this.getValue() == 'Yes')

{

//Yes then show

SHrow1.style.display = '';

//SHrow5.style.display = '';
}else{

//else hide row

SHrow1.style.display = 'none'; }

//SHrow5.style.display = 'none'; }

});
ctrlReqDet.on('change', function(e){

if (this.getValue() == 'No')

{

//Yes then show

SHrow2.style.display = '';

SHrow3.style.display = '';

SHrow4.style.display = '';

}else{

//else hide row

SHrow2.style.display = 'none';

SHrow3.style.display = 'none';

SHrow4.style.display = 'none'; }
});


However if I uncomment the SHrow5 items nothing works. And I tried taking this section:

ctrlReqDet.on('change', function(e){

if (this.getValue() == 'No')

{

//Yes then show

SHrow2.style.display = '';

SHrow3.style.display = '';

SHrow4.style.display = '';

}else{

//else hide row

SHrow2.style.display = 'none';

SHrow3.style.display = 'none';

SHrow4.style.display = 'none'; }
});



And inserting it into the true section of the first if and it would not work. Wondering if I'm doing something wrong or if its even possible to hide a row if the Javascript is using it later in the code.

C
cgphp 11/7/2011

It's hard to say what's happening without seeing your actual files. Do you get some error in firebug ?

W
wildwally author 11/7/2011

when I uncoomment the SHrow5 stuff and try running it I get this in Firebug.
SHrow5 is null

http://localhost:8085/include/runnerJS/events/pageevents_AE.js

Line 2
Maybe i need to set the value for SHrow5 to yes?
Would this be accomplished by inserting SHrow5.value = "Yes"; ?

C
cgphp 11/7/2011
if(SHrow5.getValue() == '')

SHrow5.setValue('yes');


As I said, it's hard to say what's happening without seeing your actual files. We need more info about your fields and HTML code.

Sergey Kornilov admin 11/7/2011

I guess you do not have an element with ID "answeryes" on your page. That's why SHrow5 might be null.

var SHrow5 = document.getElementById("answeryes");
W
wildwally author 11/7/2011

LOL, came back to report what i found.
After digging deeper and not understanding why the Null - I found that the anseryes id was overwritten in the process of changing something else. So Sergey nailed it on the head.
Cristian I kept having problems with your code until I had enough coffee to realize what I was doing wrong. SHrow5 is not a field, it refers to the row - completely my mistake. But I got to hand it to you two for trying to help us not so knowledgible people out...My hats off to you two.
Much appreciated.