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.
$("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('<input name="'+name+'" type="radio" value="9999" checked="checked" />');
// ^ this works
} // if
});
btw, name is set dynamically earlier in the script.
Answer:
$("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('<input name="'+name+'" type="radio" value="9999" checked="checked" />');
} // if
});
$(textfield).val("9999")?<input type="text" name="foo" />or<input type="radio" name="foo" value="bar" />