Solved

jquery get selected value where div id is unknown

Posted on 2013-10-22
4
697 Views
Last Modified: 2013-10-22
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
0
Comment
Question by:joomla
  • 2
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39593050
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

0
 

Author Comment

by:joomla
ID: 39593076
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
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 39593109
check this : http://jsfiddle.net/8SvTH/2/
$(document).ready(function() {
    $('#container').on('change', "select[name^='employee']", getvalue);    
});

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

Open in new window

You can replace : '#container'
by : document
0
 

Author Closing Comment

by:joomla
ID: 39593121
brilliant support
access to jsfiddle greatly improved my understanding of it.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question