Solved

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

Posted on 2011-09-09
6
550 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…

760 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

21 Experts available now in Live!

Get 1:1 Help Now