Click here to Skip to main content
13,800,409 members
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


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();
Posted 22-Jun-17 3:47am
Updated 22-Jun-17 7:44am

1 solution

Rate this: bad
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:

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);


See demo[^].

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 | Cookies | Terms of Service
Web02 | 2.8.181214.1 | Last Updated 22 Jun 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

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