Solved

How do I create a jQuery .change event handler at runtime?

Posted on 2011-09-09
6
567 Views
Last Modified: 2012-08-13
Hello,
I'm very new to MVC, Razor, and jQuery.
I have a dynamically created table where each row includes a dropdownlist.  I need to find out how I can create an .change event handler for each row's dropdownlist that is created.  I have attached 2 snippets of code describing the creation of the table and a jQuery function to handle a .change event.  I need to find out how I can create the .change event handler that will distinguish which dropdown list the event came from OR how to create a .change event handler at runtime.

Also, (as an extra side quesiton), how would I set corresponding fee amount to "Fee Amount" field on the table?

Thanks,

Sergio Valdez
Creation of table
-----------------------

<table width="100%" style="border:1">
	<tr>
		<th style="color: Blue; text-align: center; width: 40%">Fee Description</th>
		<th style="color: Blue; text-align: center; width: 15%">Fee Amount</th>
		<th style="color: Blue; text-align: center; width: 20%">Qty / Hours</th>
		<th style="color: Blue; text-align: center; width: 20%">Extended Total</th>
		<th style="color: Blue; text-align: center; width: 5%">Delete</th>
	</tr>
	@{var ddlNum = 0;}
	@foreach (Travis.Courts.Web.IDEP.App_Code.InvoiceData.InvoiceFeeList iFee in invoiceFeeList)
	{
		<tr>
			<td style="text-align: right; width: 40%">@Html.DropDownList("FeeDescription", iFee.FeeScheduleList, new { id = "FeeDescDDL" + ddlNum.ToString() })</td>
			<td style="text-align: center; width: 15%" id="FeeAmount">$@iFee.InvFee.UnitCost.ToString()</td>
			<td style="text-align: center; width: 20%">@Html.TextBox("Quantity_TimeInHours", iFee.InvFee.Qty_TimeinHours.ToString())</td>
			<td style="text-align: right; width: 20%">$@iFee.InvFee.ExtendedTotal.ToString()</td>
			<td style="text-align: center; width: 5%"><a href="@Url.Action("DeleteFeeItem", "InvoiceEdit", new { invId = Model.InvoiceRecord.InvoiceID, feeId = iFee.InvFee.InvoiceFeeID })" ><img src="../../Content/Images/delete_12x12.png" alt="" style="border:none;" /></a></td>
		</tr>
		ddlNum++;
	}
</table> 


jQuery function
--------------------------------
$(function () {
	$("#FeeDescDDL").change(function () {
		var ddlFeeId = "";
		$("#FeeDescDDL option:selected").each(function () {
			// set corresponding fee ID to invoice fee item
			// set corresponding fee amount to "Fee Amount" field
			ddlFeeId += $(this).val();
		});
	});
});

Open in new window

0
Comment
Question by:Courts-Tech
[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
  • 3
  • 2
6 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36510973
replace : #FeeDescDDL
by : .FeeDescDDL

and use :
	$(".FeeDescDDL").change(function () {
	//$(".FeeDescDDL").live("change",function () {
		var ddlFeeId = "";
		$(".FeeDescDDL").each(function () {
			// set corresponding fee ID to invoice fee item
			// set corresponding fee amount to "Fee Amount" field
alert($(this).val());
			ddlFeeId += $(this).val();
		});
alert(ddlFeeId);
	});

Open in new window

0
 
LVL 7

Expert Comment

by:gopaltayde
ID: 36516053
in jQuery function replace #FeeDescDDL with #FeeDescription
0
 

Author Comment

by:Courts-Tech
ID: 36524691
When I replaced #FeeDescDDL with #FeeDescription (along with removing the "new { id = "FeeDescDDL" + ddlNum.ToString()}" parameter from the DropDownList, the only change event the jQuery function captures is the very first dropdown list on my table and is ignored by the change events from the other dropdownlists.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36524752
ID attribute is unique in a valid document. You may use the same ID for multiple element (I mean no atomic explosion if you do that) but DOM function (used via jQuery or not) will failed in their job.

<<
replace : #FeeDescDDL
by : .FeeDescDDL
>>

Here I mean, instead using the same ID for all your dropdown(very bad practice), create a class for all of them .

<select class="FeeDescDDL">

Yes, it's allowed to use the same class name for multiple elements.

$(".FeeDescDDL") represent ALL your dropdown

You may use the same attribute for all your dropdown but the jQuery selector is not usual : $("select[name='FeeDescDDL']")

<select name="FeeDescDDL">
0
 

Author Comment

by:Courts-Tech
ID: 36525026
Great!  Thanks... that worked!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36525162
:)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 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