Member_2_1242703
asked on
Running a javascript on repeater items from a button or focus?
I have a form with a repeater with 3 textboxes (TextBox1, TextBox2, and TextBox3) and a button. The button is not part of the repeater.
When I click the button, I want to multiply TextBox1 and TextBox2 and have TextBox3 show the result if TextBox1 and TextBox2 are numeric, but do nothing if either TextBox1 or TextBox2 is blank or non-numeric for each line in the repeater.
Alternatively, I could add a button to each line of the repeater, or ideally run the script once the focus leaves either TextBox1 or TextBox2.
How would I achieve one of these scenarios?
When I click the button, I want to multiply TextBox1 and TextBox2 and have TextBox3 show the result if TextBox1 and TextBox2 are numeric, but do nothing if either TextBox1 or TextBox2 is blank or non-numeric for each line in the repeater.
Alternatively, I could add a button to each line of the repeater, or ideally run the script once the focus leaves either TextBox1 or TextBox2.
How would I achieve one of these scenarios?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think its missing the $
$('.cssSalary').each(funct ion () {
sum = sum + parseInt((this).html());
$('.cssSalary').each(funct
sum = sum + parseInt((this).html());
ASKER
I'm getting '$' is not defined when I leave them in
Are you using JQuery?
ASKER
Yes, I had my ref to jquery I copied from the page and mine is in a folder called scripts. I changed that and it's running without the errors, but not appearing to do anything.
Use F12 developer tools to debug the javascript to see if it is being called and what's it doing.
ASKER
Ok, It's being called. The value of sum is NaN but I filled out all of the fields with numbers.
Repeater items:
Trigger:
Jquery:
Repeater items:
<td align = "left" style="border:1px solid #A9A9A9;white-space: nowrap;"><asp:TextBox ID="tbPLAPUnits" runat="server" BorderStyle="None" class="cssSalary"></asp:TextBox></td>
<td align = "left" style="border:1px solid #A9A9A9;white-space: nowrap;"><asp:TextBox ID="tbPLUnits" runat="server" BorderStyle="None" class="cssSalary"></asp:TextBox></td>
<td align = "left" style="border:1px solid #A9A9A9;white-space: nowrap;"><asp:TextBox ID="tbTotalPieces" runat="server" BorderStyle="None" Cssclass="cssSum"></asp:TextBox></td>
Trigger:
<button type="button" onclick='calc()'>Click Me!</button>
Jquery:
<script type="text/javascript" language="javascript">
function calc() {
var sum = 0;
$('.cssSalary').each(function () {
sum = sum + parseInt($(this).html());
});
$('.cssSum').html(sum);
}
</script>
How many times is each function running? Is there any other control using cssSalary class?
ASKER
It runs through once for each item in the repeater times the number of times cssSalary is listed. In the example I'm using, there are six items and the function is run 12times. There are no other instances of cssSalary in the code aside from those listed above.
Can you give either your complete markup or page source?
ASKER
Ok, I'm just starting over here. Maybe you can help. Here's my current query...
I've got this firing, and working properly. As expected, it returns the value of zero.
So I added this to the code:
Here is the textbox in the repeater:
<asp:TextBox ID="tbPLAPUnits" runat="server" BorderStyle="None" Height="19px" CssClass="cssSalary"></asp :TextBox>
The alert still fires, but returns NaN
Just for grins, I tried this:
Which just returned a blank alert. How would I just cycle through each repeater element and display the value in the alert? Maybe if I can figure that out, I can wrap this up.
function calc() {
var sum = 0;
alert(sum);
}
I've got this firing, and working properly. As expected, it returns the value of zero.
So I added this to the code:
function calc() {
var sum = 0;
$('.cssSalary').each(function () {
sum = parseInt($(this).html());
});
alert(sum);
}
Here is the textbox in the repeater:
<asp:TextBox ID="tbPLAPUnits" runat="server" BorderStyle="None" Height="19px" CssClass="cssSalary"></asp
The alert still fires, but returns NaN
Just for grins, I tried this:
function calc() {
var sum = 0;
$('.cssSalary').each(function () {
sum = $(this).html();
});
alert(sum);
}
Which just returned a blank alert. How would I just cycle through each repeater element and display the value in the alert? Maybe if I can figure that out, I can wrap this up.
ASKER
Ok made some progress...
Here's my texboxes in my repeater:
My button that fires the jQuery:
And my current jQuery:
This will fire an alert with the total of the two textboxes. Works great. So now I need to figure out how to assign the value to the third textbox in the repeater instead of displaying it in an alert. Also I assume this will only work for 1 line in the repeater. Can I just do another "each" to make it work for every line?
Here's my texboxes in my repeater:
<asp:TextBox ID="tbPLAPUnits" runat="server" BorderStyle="None" Height="19px" CssClass="cssSalary"></asp:TextBox>
<asp:TextBox ID="tbPLUnits" runat="server" BorderStyle="None" Height="19px" CssClass="cssUnits"></asp:TextBox>
<asp:TextBox ID="tbTotalPieces" runat="server" BorderStyle="None" Height="19px" CssClass="cssTotal"></asp:TextBox>
My button that fires the jQuery:
<input type="button" id="filter" name="filter" value="Test" onclick="calc()" />
And my current jQuery:
<script type="text/javascript" language="javascript">
var sum = 0;
var sum2 = 0;
var sum3 = 0;
$('.cssSalary').each(function () {
sum = $("input[id$=tbPLAPUnits]").val();
});
$('.cssUnits').each(function () {
sum2 = $("input[id$=tbPLUnits]").val();
});
sum3 = parseInt(sum) + parseInt(sum2)
alert(sum3);
}
</script>
This will fire an alert with the total of the two textboxes. Works great. So now I need to figure out how to assign the value to the third textbox in the repeater instead of displaying it in an alert. Also I assume this will only work for 1 line in the repeater. Can I just do another "each" to make it work for every line?
ASKER
Ok, a bit more progress...
I removed the cssClass from the 2nd 2 textboxes:
Then modified the jQuery like this:
This fires of the alert with the values I need. No to just figure out how to assign those values to the 3rd textbox.
I removed the cssClass from the 2nd 2 textboxes:
<asp:TextBox ID="tbPLAPUnits" runat="server" BorderStyle="None" Height="19px" CssClass="cssSalary"></asp:TextBox>
<asp:TextBox ID="tbPLUnits" runat="server" BorderStyle="None" Height="19px"></asp:TextBox>
<asp:TextBox ID="tbTotalPieces" runat="server" BorderStyle="None" Height="19px"></asp:TextBox>
Then modified the jQuery like this:
<script type="text/javascript" language="javascript">
function calc() {
var sum = 0;
var sum2 = 0;
var sum3 = 0;
$('.cssSalary').each(function () {
sum = $(this).val();
sum2 = $(this).val();
sum3 = parseInt(sum) + parseInt(sum2)
alert(sum3);
});
}
</script>
This fires of the alert with the values I need. No to just figure out how to assign those values to the 3rd textbox.
ASKER
Ok that actually wasnt working. I was using the same numbers in each line and it was just adding them from one texbox to itself. So back to the original code:
This is giving me the total of all textboxes (cssSalary) in all the rows in the repeater. How do I get the value for each row?
<script type="text/javascript" language="javascript">
function calc() {
var sum = 0;
var sum2 = 0;
var sum3 = 0;
$('.cssSalary').each(function () {
sum = sum + parseInt($(this).val());
});
alert(sum)
}
</script>
This is giving me the total of all textboxes (cssSalary) in all the rows in the repeater. How do I get the value for each row?
ASKER
All three textboxes are in a table row. I assigned the "class" value of the row to "myRow" then modifed the jQuery like this:
Interestingly, it knows to iterate through as many times as there are rows, but it only calculates the values for the textboxes in the first row. So in this scenario there are 3 rows in the table in the repeater, I get the same value in an alert 3 times.
<script type="text/javascript" language="javascript">
function calc() {
var sum = 0;
var sum2 = 0;
var sum3 = 0;
$('.myRow').each(function () {
sum = parseInt($('.cssSalary').val());
sum2 = parseInt($('.cssSalary2').val());
sum3 = sum + sum2
alert(sum3)
});
}
</script>
Interestingly, it knows to iterate through as many times as there are rows, but it only calculates the values for the textboxes in the first row. So in this scenario there are 3 rows in the table in the repeater, I get the same value in an alert 3 times.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Got it. Added this
$('#popup_Repeater4_ctl0' + i + '_tbTotalPieces').val(total)
Glad you got it working :-)
ASKER
Answered myself
ASKER
Open in new window
Open in new window
Returns...
Object doesn't support property or method 'each'
I've also tried putting the onclick='calc()' in the TD like in the example...same result.