Solved

jquery get selected value where div id is unknown

Posted on 2013-10-22
4
740 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

690 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