1

I need to get the value of input[name="color_cost[0]"]

I have a jQuery script like this but the colorCost object is undefined

var colorCost = $('input[name="color_cost[0]"]').val();

var colorCost = $('input[name="color_cost[0]"]').val();
console.log(colorCost);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" name="color_cost[]" value="100">
<input type="text" name="color_cost[]" value="200">
<input type="text" name="color_cost[]" value="300">
<input type="text" name="color_cost[]" value="300">

1
  • the 0 in the selector won't work. Commented Nov 23, 2017 at 20:37

3 Answers 3

1

The 0 changes the selector. Use .eq(index) to get the one you want.

var colorCost = $('input[name="color_cost[]"]').eq(0).val();
console.log(colorCost);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" name="color_cost[]" value="100">
<input type="text" name="color_cost[]" value="200">
<input type="text" name="color_cost[]" value="300">
<input type="text" name="color_cost[]" value="300">

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

Comments

0

You can use .serializeArray()

You don't need to specify input in the selector neither (if unique)

var colorCost = $('[name="color_cost[]"]').eq(0).val();
var colorCostArray = $('[name="color_cost[]"]').serializeArray()

console.log(colorCost)
console.log(colorCostArray[0].value)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" name="color_cost[]" value="100" size="3">
<input type="text" name="color_cost[]" value="200" size="3">
<input type="text" name="color_cost[]" value="300" size="3">
<input type="text" name="color_cost[]" value="300" size="3">

Comments

0

You are using a wrong selector if you put 0 between the brackets, it should be 'input[name="color_cost[]"]', it will give you a collection of elements.

var colorCost = $($('input[name="color_cost[]"]')[0]).val();
console.log(colorCost);

Then with $('input[name="color_cost[]"]')[0] you can get the first one, and wrap it inside $() so it's read as jQuery object, and you can use .val() with it.


And if you want to get all the inputs values in an array, you can use:

var inputs = Array.from($('input[name="color_cost[]"]')).map(function(input){
    return $(input).val();
});

Demo:

var colorCost = $($('input[name="color_cost[]"]')[0]).val();
console.log(colorCost);

var inputs = Array.from($('input[name="color_cost[]"]')).map(function(input) {
  return $(input).val();
});
console.log(inputs);
console.log(inputs[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" name="color_cost[]" value="100">
<input type="text" name="color_cost[]" value="200">
<input type="text" name="color_cost[]" value="300">
<input type="text" name="color_cost[]" value="300">

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.