Goldie
Goldie

Reputation: 1630

jQuery check for existence of input after remove()

I have a form where a user is supposed to select predefined data add new data in an input field named xxx and then he/she can proceed further.

When predefined data is selected, a new hidden input is created

$('.selectlists').append('<input type="hidden" name="select" value="1" />')

I have a function which checks if the hidden field exists and if input xxx has any value

function checkAll(){
        if( $("input[name*='select']") ){
            alert('ok');
        }
        else{
            if ( $("input[name*='xxx']").val() != '') {
                alert('ok');
            }
        }
    }

The function is working fine. But if I create the hidden input and then remove it with jQuery's function remove(), my function checkAll() is showing that the hidden input still exists.

Upvotes: 1

Views: 1471

Answers (6)

thecodeparadox
thecodeparadox

Reputation: 87073

function isexists(){
   var hiddenLen = $(".selectlists input[name*='select']:hidden").length,
       hasval = $.trim( $("input[name*='xxx']").val() ).length;
   return {
      hiddedexists: !len,
      hasval : !hasval
    };
}

You can use this function like:

var exists = isexists();

exists.hiddenexists; // true of false
exists.hasval;  // true of false

Note

!len will give true if len=0 else false.

Upvotes: 0

sejal patel
sejal patel

Reputation: 272

you can try this

 if( jQuery("*[name='select']") ){
            alert('ok');
        }

Upvotes: 0

Your check will show an alert box with 'ok', when the input field is not present because

$("input[name*='xxx']").val()

Will return 'undefined' and not match empty string.

You can check for the existence of elements by checking if the jQuery object return any hits OR check for 'undefined'

Either of these should work:

typeof $('input[name=xxx]').val() === 'undefined'

or

$('input[name=xxx]').length === 0

I would prefer option 2. because it seems more robust to changes in jQuery.

Upvotes: 0

fiatjaf
fiatjaf

Reputation: 12158

use the length property for testing if some element exists, because the function $() returns an array of objects and in javascript all arrays evaluates to true, even if they are empty, which is the case.

if( $("input[name*='select']").length ){
     alert('ok');
}

This way, if there's no input[name*='select'] the length will be zero and the expression will evaluate to false.

Upvotes: 0

Snake Eyes
Snake Eyes

Reputation: 16764

you create input like

$('.selectlists').append('<input type="hidden" name="select" value="1" />').

Right ?

$('.selectlists input[name="select"]').remove();

try in checkAll method:

if( $(".selectlists input[name='select']") ){
            alert('ok');
        }

or

if( $(".selectlists input[name='select']").length ){
            alert('ok');
        }

Upvotes: 1

Yoeri
Yoeri

Reputation: 1906

if($("input[name*='select']")) ...

should check for the length ...

if( $("input[name*='select']").length > 0) 

JQuery returns an array of matching elements.

Upvotes: 3

Related Questions