Javascript + jquery check if the item exists, then set its value, otherwis...

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
});
我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章