Solved

jquery get selected value where div id is unknown

Posted on 2013-10-22
4
686 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now