Solved

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

Posted on 2011-09-09
6
563 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
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

828 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