4

Is it possible to get value of all elements return by find or children method without loop?

There is multiple li that contai hidden field and we want to get all these hidden field val.

i.e. var cats = $(this).next('ul').find('.hdn_id').val();

But its return only single value.

0

2 Answers 2

3

You need to iterate the elements, For simplicity .map() can be used.

Pass each element in the current matched set through a function, producing a new jQuery object containing the return values.

var cats = $(this).next('ul').find('.hdn_id').map(function () {
    return $(this).val();
}).get();

As the return value is a jQuery object, which contains an array, it's very common to call .get() on the result to work with a basic array.

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

2 Comments

Is it possible to covert output array to NUM array?
@GovindSamrow, You can use return Number($(this).val()); or return parseFloat($(this).val());
0
.contents()
.filter(function(){
return this.nodeType !== 1;
})

Comments

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.