0

I have a code

HTML :

<input type="hidden" name="test[]" id="test_0" value="123456789">

Javascript :

$(".test").each(function(){
   console.log('Test Value : '+ $(this).val());
});

Result :

Test Value =

The question :

Why result of console.log for $(this).val() inside my Javascript code give result empty ? And how to fix it without change the Javascript code?

Thank you

4
  • The code shown would not log anything to the console, because you have no elements with the "test" class. If you are seeing "Test Value =" in the console there must be other elements not shown. Commented Feb 17, 2017 at 2:01
  • @nnnnnn : so what is your solution to fix that? Commented Feb 17, 2017 at 2:02
  • My point is that the result you say you are seeing is not possible with the code shown. Are you asking how to get the value of a single, specific input? If so, how do you want to identify that input? By id? By name? Or...? Commented Feb 17, 2017 at 2:04
  • @nnnnnn : if i don't want to change the Javascript but only change the HTML so how to fix it? you can post as an answer , if it solve the problem i will accept as correct answer. thank you Commented Feb 17, 2017 at 2:05

4 Answers 4

2

The problem is that $(".test") targets the class test, not the name test. In order to solve that by only changing the HTML, simply give the input the relevant class:

<input type="hidden" name="test[]" class="test" id="test_0" value="123456789">

A working fiddle demonstrates this here.

Hope this helps :)

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

Comments

0

test class is not defined.

try example,

console.log('Test Value : '+ $('#test_0').val());

1 Comment

that's correct, but i don't want to change Javascript, you only can change the HTML code.
0

You are not targeting the desired element.

Try this:

$("#test_0").each(function(){
   console.log('Test Value : '+ $(this).val());
});

2 Comments

that's correct, but i don't want to change Javascript, you only can change the HTML code
If you want to change the HTML, add class="test" to your <input>
0

In the HTML, you have to add a class to input

The problem, is that you are not getting any element, because you haven't any element with that class. I think that's all.

1 Comment

<input class="test" type="hidden" name="test[]" id="test_0" value="123456789">

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.