[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2011-09-09
6
Medium Priority
?
588 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…
Suggested Courses

650 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