0

Hence i have two array input fields like

<tbody value='question'>
<tr> <td><input type="text" name="question[one][]" ></td> <td><input type="text" name="question[one][]" >
<tr> <td><input type="text" name="question[two][]" ></td> <td><input type="text" name="question[two][]" >
</tbody>

And another is

<tbody value='answer'>
<tr> <td><input type="text" name="answer[one][]" ></td> <td><input type="text" name="question[one][]" >
<tr> <td><input type="text" name="answer[two][]" ></td> <td><input type="text" name="question[two][]" >
</tbody>

I am getting data upon submit like this.

question => array [ => "one" => array [ 'value1','value2'], "two" => array ['value1','value2']]

and

answer => array [ => "one" => array [ 'value1','value2'], "two" => array ['value1','value2']]

That is fine. This is just the data structure.

Now in the form i want to auto-complete the answer fields upon question input fields is fill up on the from. Or after complete the question fields and click on a button like use this as answer the event trigger and answer filed will filled up with the question data. And it is assure that the question and answer input field is the same format. only the name change.

I tried jQuery With The val() Method failed on undefined variable.

Please help me out!

4
  • Blocks of code should be indented with at least 4 spaces. And that's not JSON. Commented Nov 7, 2016 at 20:53
  • you can do that with javascript Commented Nov 7, 2016 at 20:54
  • Ok. @Pointy thank you for edit. I will remember next time. Commented Nov 7, 2016 at 22:28
  • @ErikKalkoken I know bro. But i got undefined error. Commented Nov 7, 2016 at 22:34

1 Answer 1

1

I'm not sure what you want, so correct me if I misunderstand something

First, your json looks like:

question = {
  one: [ 'value1','value2'],
  two: ['value1','value2']
}
answer = {
  one: [ 'value1','value2'],
  two: ['value1','value2']
}

Second, you want to fill inputs with values from json:

$('#button').click(function() {
  $("input[name='answer[two][]']").val(answer['two'][0]);
});

question = {
  one: [ 'value1','value2'],
  two: ['value1','value2']
}

$('#button').click(function() {
  $("input[name='question[one][0]']").val(question['one'][0]);
  $("input[name='question[one][1]']").val(question['two'][1]);  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<tbody value='question'>
<tr> <td><input type="text" name="question[one][0]" ></td> <td><input type="text" name="question[one][1]" >
<tr> <td><input type="text" name="question[two][0]" ></td> <td><input type="text" name="question[two][1]" >
</tbody>
<button id="button">click me!</button>

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

4 Comments

This is like question : [ one: [ 'value1','value2'], two: ['value1','value2'] ] got undefined error
What's wrong? Did you run code snipped? Is that what you want?
Since i fill up the question input and click the button on the same page where answer input form .. Its giving an error. ReferenceError: question is not defined <anonymous> n.event.dispatch() n.event.add/r.handle()
I mean its one form.. Where question is an array filed and answer is an array field. And after i filled up question on top and click on button like checkbox then answer will be filled up with those array data.. and there is exact same amount of array field while the name is one is question and another answer

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.