Inserting a condition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    Inserting a condition

    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?


    Code:
    		var cKeyChk = ( cOrder.indexOf("ID") == -1 ) ? true:false;
    
            tr = tbody.insertRow(tbody.rows.length);
    		td = tr.insertCell(tr.cells.length);
    		td.setAttribute("nowrap", "nowrap");
    		td.setAttribute("align", "right");
    		td.innerHTML =	'<INPUT'+
    							' type="radio"'+ 
    							' name="SearchMethod"'+ 
    							' id  ="Contains"'+
    							' value="Contains"'+
    							if (cKeyChk)
    							{
    							' checked'+
    							}
    						'/>';
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You can't use a condition in a string like that. Use a string variable instead, e.g.
    Code:
    		var cKeyChk = ( cOrder.indexOf("ID") == -1 ) ? true:false;
    var cKeyChkStr = "";
    if (cKeyChk) cKeyChkStr = " checked";
    //...
    		td.innerHTML =	'<INPUT'+
    							' type="radio"'+ 
    							' name="SearchMethod"'+ 
    							' id  ="Contains"'+
    							' value="Contains"'+
    							cKeyChkStr+
    						'/>';

    Comment

    • Logician
      New Member
      • Feb 2007
      • 210

      #3
      Code:
      td.innerHTML =  '<INPUT'+' type="radio"'+ 
      ' name="SearchMethod"'+
      ' id  ="Contains"'+
      ' value="Contains"'+
      (cKeyChk ?' checked':'')+ '/>'
      Since you're using DOM methods already, instead of .innerHTML:

      Code:
      td.appendChild(document.createTextNode(/*string here*/))

      Comment

      • Claus Mygind
        Contributor
        • Mar 2008
        • 571

        #4
        Thank you both for excellent advise. I like it when it can be done short and neat.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by Logician
          Code:
          ' value="Contains"'+
          (cKeyChk ?' checked':'')+ '/>'
          Nice one- forgot about that.

          Originally posted by Logician
          Since you're using DOM methods already, instead of .innerHTML:
          Code:
          td.appendChild(document.createTextNode(/*string here*/))
          Well, that would only print out a string, unless you meant that as an example only.

          Comment

          Working...