Link to home
Start Free TrialLog in
Avatar of joomla
joomlaFlag for Australia

asked on

jquery get selected value where div id is unknown

I have a number of "select" fields that are created dynamically.
The fields will have the same name, although the id can be different

<select name=employee[] id=employee_1>
<select name=employee[] id=employee_2>

I want to use jquery to get a handle on the value each time it changes.

If I know the id of the object I can call a routine to get the value

function getvalue(){
     $("#employee_1").val();
}

But I don't know how many of these dynamic fields may be created at any time.
is there a way to pass the object id to the jquery script

for example.....
function getvalue(this){
      $(this.id).val();
}

thanks
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Test page : http://jsfiddle.net/8SvTH/1/
$(document).ready(function() {
    $("select[name^='employee']").change(getvalue);    
});

function getvalue() {
    var id = $(this).attr('id');
    var value = $(this).val();
    alert('id:'+id+'\nvalue:'+value)
}

Open in new window

Avatar of joomla

ASKER

Hi,
I like the solution and the jsfiddle makes it simple to test.

I have a challenge
The fields are being created dynamically.
As such the routine doesn't execute when the value changes ?

thanks
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joomla

ASKER

brilliant support
access to jsfiddle greatly improved my understanding of it.