1

I have an input type hidden in a loop, so it's a multidimensional array and it has one input name.
How can I check if there is any value in it? Like any value or element?

This is the code: (the array is generated in php in a loop)

echo "<td class='$border'>
    <div class='DropZonePers'></div>
    <input type='hidden' value='' name='pers[$dag][$j]'/>
</td>";

What I have tried in js:

if($(".pers").length !> 0) {
   alert("No!");
   return false;
}

How can I do that?

4
  • Please remove the "C" tag from this question; it has nothing to do with C. Commented May 13, 2015 at 22:15
  • I just did, I didn't notice the tag Commented May 13, 2015 at 22:15
  • @anantkumarsingh dude I can't put the name as a value, the value is created in a drag & drop system Commented May 13, 2015 at 22:25
  • Sorry for misunderstanding. Commented May 13, 2015 at 22:26

2 Answers 2

2

grab all input elements that have a name that starts with "pers" and see if any are not empty

function persIsEmpty() {
    var empty = true;
    $('input[type="hidden"][name^="pers"]').each(function () {
        if ($(this).val().length > 0 && $(this).val() != '') {
            empty = false;
        }
    });
    return empty;
}

//...so something like

if (persIsEmpty()) {
    alert("No!");
    return false;
}

See: https://css-tricks.com/attribute-selectors/

Sign up to request clarification or add additional context in comments.

Comments

0

$(".pers") searches for an element with class name "pers". Your hidden input does not have that class name. The following will search for the input with type="hidden" and check to see if its value is an empty string.

if ($('input[type="hidden"]').val() == '') {
    alert("No!");
    return false;
}

NOTE: If you have more than one hidden input, and you want to uniquely search for the input specified in your post, give it an id or a unique class to search by.

With class:

$('.className').val()

With ID:

$('#IDName').val()

1 Comment

I can't do that because some of the input might be empty, the user is not supposed to fill in all the values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.