Click here to Skip to main content
13,199,852 members (65,677 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Currently, I had the following code. I want to assign value of select option into text input. I don't know how to loop it as I got many of the select option.

Here is the fiddle

For example, if I select

1.a.=4 , 1.b=3 , 1.c=2 , 1.d=1,

then my text input there if will show

1.

4 K

3 A

2 V

1 Ad

and so on for every question.Thank you.I had tried my code and it just detect the last column.

What I have tried:

$('select[name=qtyQ5]').change(function() {
        $('select[name=qtyQ5] :selected').each(function() 
		{
			var $this = $(this).val();
            $(".numQ5").each(function()
            {	
            	$(this).val($this);
            });
        });
    });
Posted 22-Jun-17 2:47am
Updated 22-Jun-17 6:44am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You got what you ask for with your code. Each iteration of the outer loop, the inner loop simply overwrites all the text boxes with the next selected value.
Look at the DOM hierarchy of each set of select elements:
<ol>
 <li><select>...</select></li>
 <li><select>...</select></li>
 <li><select>...</select></li>
 <li><select>...</select></li>
</ol>

What you need is to transfer the select value to the corresponding text box of the same index position, got it?
So the first step is to find out the index number of the li where the select change take place within the ol, then transfer the selected value to the text box with the same index number in the class. This is the code that do this:
$('select[name=qtyQ5]').change(function() {
 
    // find the closest li of the changed select
    $li = $(this).closest('li');
    // Find the closest ol then all its lis
    $lis = $li.closest('ol').find('li');
    // find the index of the li of the changed select
    i = $lis.index($li);
    
    $('.numQ5').eq(i).val($(this).val());
 
});

See demo[^].
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.171020.1 | Last Updated 22 Jun 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100