2

This is my ajax call

jQuery.post(ajaxurl, data, function(response) {
    var slug = $(response).find(".return_product_cat").val();
    alert(slug);
});

This is the html being returned

<input type='hidden' class='return_product_cat' value='test_cat' /> 

When i alert it says undefined but it should say test_cat? How do i solve?

2 Answers 2

3

find() searches for children, use filter()

var slug = $(response).filter(".return_product_cat").val();

Just val() would work too, unless there is more to the html.

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

4 Comments

There is no need of filter, it will be bit slower
Slower? How? Please prove it. :) Also, I have already mentioned Just val() would work too, unless there is more to the html. filter() would be needed if there is more html.
@ShaunakD Because you're calling an extra function instead of just val() it'll be bit slower
Doesn't matter if there is less html. <10^-2 ms
2

You don't need find, since the element itself is returned. find() is used to get the descendant.

Use this:

var slug = $(response).val();

DEMO

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.