Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to write the nested ifelse in javascript

Status
Not open for further replies.

compu66

Programmer
Dec 19, 2007
71
US
hi all,

can any one help me out with it.
Thanks
 
I mean is this right


if (window.opener.document.all['txtPrimChargeUnit'] == '[object]') {
//window.opener.document.all['txtPrimChargeUnit'].value = '1';
if(units=='null'){
window.opener.document.getElementById('txtPrimChargeUnit').value = '1'
}
else {
window.opener.document.getElementById('txtPrimChargeUnit').value = units

}
}
 
Well... you indentation is completely unintuitive and honestly an utter wreck, so that's probably where you're having problems understanding it. Syntactically, the code seems fine though.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
No, it's not right. You're using document.all rather than document.getElementById. The former is IE only, used to be important back in version 4 and early 5, but now just shows an unfamiliarity with modern browsers and the DOM.

If something doesn't work, you have to say what doesn't work rather than trying to get us to guess.

Lee
 
Yes [navy]trollacious[/navy], but I think [navy]kaht[/navy] ment that the if/else works fine.
I would remove the [tt][maroon]== '[object]'[/maroon][/tt] or maybe use the [tt][maroon]typeof[/maroon][/tt] function.
Maybe you could use the colon condition (condition ? if_true : if_false). Sorry I don't remember what it is called, LOL.

Code:
var win_doc = window.opener.document;
  if (win_doc.getElementById('txtPrimChargeUnit')) {
  var txt_el = win_doc.getElementById('txtPrimChargeUnit');
  txt_el.value = (units=='null' ? '1' : units);
  }


- Lowet

[gray]Why can't all browsers parse pages the same way? It should be the Web designer who decides how to display the content, not the browser![/gray]
 
Sorry I don't remember what it is called, LOL.

The term you're looking for is Ternary Operator

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
I understood quite well, Lowet.

I've always considered the ternary operator something that modern programmers generally use to be cutesy or make it look like they're advanced. That said, I've been working with C/C++ and that family of languages since the end of 1991. Back in the olden days it was used to produce smaller source code, and possibly executable, but I think it makes it more difficult for (less experienced) programmers to read and maintain the code.

Lee
 
Thank you [navy]kaht[/navy].
And yes, [navy]trollacious[/navy]. It can look too advanced for less experienced coders, but it's also good to learn it.
I use the Ternary Operator quite often when dealing with only true/false statements and in this case I thinks it's good suited.
So use this then:

Code:
var win_doc = window.opener.document;
  if (win_doc.getElementById('txtPrimChargeUnit')) {
  var txt_el = win_doc.getElementById('txtPrimChargeUnit');
    if (units == 'null') {
    txt_el.value = '1';
    }
    else {
    txt_el.value = units;
    }
  }

BTW, [navy]compu66[/navy]. Is [tt][maroon]'null'[/maroon][/tt] supposed to be a string or did you mean is as NULL?


- Lowet

[gray]Why can't all browsers parse pages the same way? It should be the Web designer who decides how to display the content, not the browser![/gray]
 
Well, his code works but maybe it does not work as he wants it to.

- Lowet

[gray]Why can't all browsers parse pages the same way? It should be the Web designer who decides how to display the content, not the browser![/gray]
 
You can't fix a maybe. That's why the OP needs to state what is wrong.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top