Solved

jquery get selected value where div id is unknown

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
jQuery Generalization 3 53
base64 decode encode 12 97
Google  (Get  Users Email) 2 65
Re-position the objects 7 51
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

758 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

20 Experts available now in Live!

Get 1:1 Help Now