2013. 10. 24. 14:04

  if($('input#b_write_title').is(":empty")) {

alert('제목을 입력 해주세요');

return;

}

if($('textarea#b_write_content').is(":empty")) {

alert('본문을 입력해주세요');

return;

}



194down voteaccepted
$('#apply-form input').blur(function()
{
    if( !$(this).val() ) {
          $(this).parents('p').addClass('warning');
    }
});

And you don't necessarily need .length or see if its >0 since an empty string evaluates to false anyway but if you'd like to for readability purposes:

$('#apply-form input').blur(function()
{
    if( $(this).val().length == 0 ) {
        $(this).parents('p').addClass('warning');
    }
});

If you're sure it will always operate on a textfield element then you can just use this.value.

$('#apply-form input').blur(function()
{
      if( !this.value ) {
            $(this).parents('p').addClass('warning');
      }
});

Also you should take note that $('input:text') grabs multiple elements, specify a context or use thethis keyword if you just want a reference to a lone element ( provided theres one textfield in the context's descendants/children ).


Posted by hoonihoon