1

Hi i have string value of <input type="hidden" name="SaleDetail[7835].ProductID" value=7835 /><input type="hidden" name="SaleDetail.Index" value="7835" />

but when I run below to extract value from input, it just returns undefined ..

var stringVal = '<input type="hidden" name="SaleDetail[7835].ProductID" value=7835 /><input type="hidden" name="SaleDetail.Index" value="7835" />'
var inputVal = $('input[name="SaleDetail.Index"]', $(stringVal)).val();

Can anyone help ?

1 Answer 1

1

Your code would search for nested children element since there are no nested children it won't return any element. To get the element from a collection of element use filter() method instead.

var stringVal = '<input type="hidden" name="SaleDetail[7835].ProductID" value=7835 /><input type="hidden" name="SaleDetail.Index" value="7835" />'
var inputVal = $(stringVal).filter('input[name="SaleDetail.Index"]').val();

console.log(inputVal);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


Or you need to wrap the elements by any other element to make it work.

var stringVal = '<input type="hidden" name="SaleDetail[7835].ProductID" value=7835 /><input type="hidden" name="SaleDetail.Index" value="7835" />'
var inputVal = $('input[name="SaleDetail.Index"]', '<div>' + stringVal + '</div>').val();

console.log(inputVal);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.