title: javascript+jquery check if element exists, then set its value, else something else
body: <p>I'm trying to check if a textfield exists withing another element, and I want to set its value if it does; otherwise, I want to add a radio. I've got the following code, but it's not working.</p>
  
  <pre><code>$("ul").each(function(){
  if ( !($(this).find('input[value="9999"]').val()) ) {
      var textfield = $(this).find('input[type="text"]');
      if ( textfield.length ) textfield.value="9999";
      // ^ this doesn't work. have also tried if ($(textfield).val())
      else $(this).append('&lt;input name="'+name+'" type="radio" value="9999" checked="checked" /&gt;');
      // ^ this works
  } // if
  });
  </code></pre>
  
  <p>btw, <code>name</code> is set dynamically earlier in the script.</p>
  
  <p><strong>Answer</strong>:</p>
  
  <pre><code>$("ul").each(function(){
  if ( !($(this).find('input[value="9999"]').val()) ) {
      var txtfield = $(this).find('input[type="text"]');
      if ( txtfield.length ) $(txtfield).val("9999");
      // just changed this ^ line (above)
      else $(this).append('&lt;input name="'+name+'" type="radio" value="9999" checked="checked" /&gt;');
  } // if
  });
  </code></pre>