By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

Inserting a condition

Claus Mygind
100+
P: 571
I want one or the other of two radio buttons to be checked depending on a substring of an incoming value.

if the variable cOrder contains ID in the string then I want the first radio button checked if not then I want the second radio button checked.

In the sample code below I only show one radio button.

I am just not sure how to construct the td.innerHTML string?


Expand|Select|Wrap|Line Numbers
  1.         var cKeyChk = ( cOrder.indexOf("ID") == -1 ) ? true:false;
  2.  
  3.         tr = tbody.insertRow(tbody.rows.length);
  4.         td = tr.insertCell(tr.cells.length);
  5.         td.setAttribute("nowrap", "nowrap");
  6.         td.setAttribute("align", "right");
  7.         td.innerHTML =    '<INPUT'+
  8.                             ' type="radio"'+ 
  9.                             ' name="SearchMethod"'+ 
  10.                             ' id  ="Contains"'+
  11.                             ' value="Contains"'+
  12.                             if (cKeyChk)
  13.                             {
  14.                             ' checked'+
  15.                             }
  16.                         '/>';
  17.  
Jun 27 '08 #1

✓ answered by Logician

Expand|Select|Wrap|Line Numbers
  1. td.innerHTML =  '<INPUT'+' type="radio"'+ 
  2. ' name="SearchMethod"'+
  3. ' id  ="Contains"'+
  4. ' value="Contains"'+
  5. (cKeyChk ?' checked':'')+ '/>'
  6.  
Since you're using DOM methods already, instead of .innerHTML:

Expand|Select|Wrap|Line Numbers
  1. td.appendChild(document.createTextNode(/*string here*/))

Share this Question
Share on Google+
4 Replies


acoder
Expert Mod 15k+
P: 16,027
You can't use a condition in a string like that. Use a string variable instead, e.g.
Expand|Select|Wrap|Line Numbers
  1.         var cKeyChk = ( cOrder.indexOf("ID") == -1 ) ? true:false;
  2. var cKeyChkStr = "";
  3. if (cKeyChk) cKeyChkStr = " checked";
  4. //...
  5.         td.innerHTML =    '<INPUT'+
  6.                             ' type="radio"'+ 
  7.                             ' name="SearchMethod"'+ 
  8.                             ' id  ="Contains"'+
  9.                             ' value="Contains"'+
  10.                             cKeyChkStr+
  11.                         '/>';
  12.  
Jun 27 '08 #2

100+
P: 210
Expand|Select|Wrap|Line Numbers
  1. td.innerHTML =  '<INPUT'+' type="radio"'+ 
  2. ' name="SearchMethod"'+
  3. ' id  ="Contains"'+
  4. ' value="Contains"'+
  5. (cKeyChk ?' checked':'')+ '/>'
  6.  
Since you're using DOM methods already, instead of .innerHTML:

Expand|Select|Wrap|Line Numbers
  1. td.appendChild(document.createTextNode(/*string here*/))
Jun 27 '08 #3

Claus Mygind
100+
P: 571
Thank you both for excellent advise. I like it when it can be done short and neat.
Jun 27 '08 #4

acoder
Expert Mod 15k+
P: 16,027
Expand|Select|Wrap|Line Numbers
  1. ' value="Contains"'+
  2. (cKeyChk ?' checked':'')+ '/>'
Nice one- forgot about that.

Since you're using DOM methods already, instead of .innerHTML:
Expand|Select|Wrap|Line Numbers
  1. td.appendChild(document.createTextNode(/*string here*/))
Well, that would only print out a string, unless you meant that as an example only.
Jun 28 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.