qqruza
qqruza

Reputation: 1417

Form input with empty value

I have got a problem when trying to identify an INPUT with empty value or which has got just value attribute without ="". Please see below my code.

<form id='form'>
<div class='input-field col s12'>
    <input id='title' type='text' value>
    <label for='title'>Job Tilte</label>
</div>
<div class='input-field col s12'>
    <input type='text' id='company' value='test' />
    <label for='company'>Company Name</label>
</div>
<div class='input-field col s12'>
    <input type='text' id='telephone' value='' />
    <label for='telephone'>Telephone Number</label>
</div>
<div class='input-field col s12'>
    <textarea id='description' placeholder='test2'></textarea>
    <label for='description'>About you</label>
</div>
<div class='input-field col s12'>
    <input type='password' id='password' />
    <label for='password'>Your password</label>
</div>
<div class='input-field col s12'>
    <input type='password' id='re-password' />
    <label for='re-password'>Confirm your password</label>
</div>
<input type='submit' disabled='disabled' value='Update' id='sendbutton' />

And this is my jQuery:

if ($('#form input').val().length !== 0 ) {
        $(this).hide();
        } else {
        $(this).next().addClass('active');
        }

Could anybody help me with this issue? Many thanks in advance.

Upvotes: 0

Views: 55

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

You need

$('#form input').each(function () {
    if (this.value.length !== 0) {
        $(this).hide();
    } else {
        $(this).next().addClass('active');
    }
})

Upvotes: 1

Nishit Maheta
Nishit Maheta

Reputation: 6031

use below code . each() you can get all the inputs elements inside #form . see DEMO

  $(document).ready(function(){
   $('#form input').each(function(){
     if ($(this).val().length !== 0 ) {
        $(this).hide();
     } else {
       $(this).next().addClass('active');
     }
  });
 });

Upvotes: 1

Related Questions