|
Good answer. +5
Make it work. Then do it better - Andrei Straut
|
|
|
|
|
Just like Bob said, the easiest/quickest way to get the index of the clicked row (in case you need the number for something else) is to add an attribute to the TR elements - you can then query this value in your onclick handler.
Drawing on the code I wrote earlier, here's a dynamic solution.
------------------ tblTest.php ----------------------
<!doctype html>
<html>
<head>
<script>
function myRowClickFunction()
{
alert('row clicked');
}
function myBtnClicked()
{
alert('btn clicked');
}
function rowClickedToggleCheckBox()
{
var clickedRow = this;
var checkBox = clickedRow.getElementsByTagName('input')[0];
checkBox.checked = (!checkBox.checked);
alert('Index of clicked row: ' + clickedRow.getAttribute('rowIndex'));
}
function addBtnToRow()
{
td = document.createElement('td');
btnInput = document.createElement('input');
btnInput.setAttribute('value', 'click me');
btnInput.setAttribute('type', 'button');
btnInput.onclick = myBtnClicked;
td.appendChild(btnInput);
this.appendChild(td);
this.onclick = null;
}
function attachEventHandlerToTableRows(functionToAttach)
{
var i, num;
var tableRows = document.getElementsByTagName('tr');
num = tableRows.length;
for (i=0; i<num; i++)
{
tableRows[i].onclick = functionToAttach;
}
}
function myInit()
{
attachEventHandlerToTableRows(rowClickedToggleCheckBox);
}
</script>
</head>
<body onload='myInit();'>
<table>
<tbody>
<?php
for ($i=0; $i<10; $i++)
{
echo "<tr rowIndex='$i'><td><input type='checkbox' id='cb_$i'></td><td>click to toggle checkBox</td></tr>";
}
?>
</tbody>
</table>
</body>
</html>
Make it work. Then do it better - Andrei Straut
|
|
|
|
|
Beautiful! I can think of many places where this would be useful. Bravo on your explanation and great example! Clean, concise, and well documented. I don't usually get to do too much web development, but just spent a week immersed in it adding features to a legacy ASP application. It never fails to amaze me how much can be handled on the clent side. I think you might only be limited by your imagination.
"Go forth into the source" - Neal Morse
|
|
|
|
|
Hi, I want to make ref of jquery.js through c# code and then want to create a function in C# to which i want to pass object as parameter. That function will do tasks that JQuery do. f.g Button as object and do animations on button using Jquery with C#(function). Thanks in advance.
|
|
|
|
|
I don't quite understand what you want to do.
1. Reference jquery in your webform
2. Create a function in code behind
3. code behind function passes parameters to a jquery script
4. Jquery then runs the parameters
I think you need to change your thinking on this.
Jquery can go to the server to get parameters.
Code Behind can prepare parameters on page load, then jquery can read them.
Something has to trigger a jquery function.
jquery is essentially javascript, just a little easier to pick up at first.
You really need to get started first, and learn how to use javascript before you dive into jquery, start below.
http://jquery.com/[jquery^]
|
|
|
|
|
Hi, I am an intermediate user with JQuery.
Actually I want to create a class in c# that will handle Jquery functionality.
My purpose regarding this is :
1> If someone dont know jquery then one can use my C# funtions for animation or other tasks that jquery has. My class will call jquery functions and ll return output.
2> Easy to debug the code using vs-degugger.
|
|
|
|
|
I write server control dll's, in which I embed scripts, and load the appropriate ones on demand, during the page.init life cycle. And then I create controls, that wire up to the scripts. When the page loads, everything is functional.
If you just had a pin-point question, with a code sample, then that's easy to answer, but in your case, your looking for someone to engineer it for you, and I don't think a class is sufficient for your needs.
I don't have an answer to offer, because I think the concept is flawed, or it exist on a super advanced level of programming.
|
|
|
|
|
I don't think you understand how ASP.net works. This isn't entirely your fault because ASP.net does its best to hide the client/server nature of websites.
Back end (C#) code is executed on the server. Its purpose is to generate markup which is sent to the browser, which can include scripts and links to scripts (e.g. jQuery). Once a request has been handled and markup sent back to the browser, your back end code is not running any more.
jQuery (like other JavaScript code) is run in the browser, and allows you to modify what the client sees or does. Anything interactive that happens in the browser is done with JS.
The only ways to communicate between them are HTTP requests, either through loading a new page with an <a> or <form action=...> tag, or via an AJAX request.
Animation is a client side task and therefore it will always be done with JavaScript. There is simply no (sane) way to get back end code involved in that code cycle, because the animation is done in the browser and the server is not involved.
|
|
|
|
|
That's a good explanation you wrote there Bob.
I guess he wants to write a class in which if you want to animate an object, you just call his class and the object, and he does the rest.
I don't think just a class is big enough for that, so he's needs some serious engineering there on how to build it.
You and I could actually write something like that, but I don't have a need for that at this moment in time.
But I have my thinking cap on now, and if I needed it today, I would attempt to write a jquery plugin, that you just attach to the object, to call the simple animations needed by the user.
So that's the answer, write a jquery plugin then. I forget the OP's Name, so OP, I'd leave the asp.net out of this, and look into writing a plugin.
Here's a link with some sample code to get you started today!
http://docs.jquery.com/Plugins/Authoring[^]
|
|
|
|
|
I am building a project managent information system and I need a texteditor in some of my pages. How do i go about having it work in my application.
|
|
|
|
|
First you find one that fits your purposes, then you download it and add any components/JavaScript references/CSS files that it needs. Finally, you add it to your page and call any initialisation routines.
That's as specific as I can get right now. The editor you include should have samples and documentation that show how to add it into your site.
|
|
|
|
|
<textarea>
|
|
|
|
|
|
This[^] is an easy one to implement.
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair.
nils illegitimus carborundum
me, me, me
|
|
|
|
|
|
Hello ,
here is my issue , I would like to be able to set a checkbox named mycheckbox to yes by inserting a script in an external webpage.
let's say i want this check box to be check when i execute the code in the website:
www.somewebsite.com
Many thanks.
|
|
|
|
|
document.getElementById("mycheckbox").checked = true;
|
|
|
|
|
I wrote this, and after changing the value of the dropdown like 20 times, I noticed the function got slower and slower until I was not able to select a value after about 6 times. I tried optimizing it, it was 3 times, now 6.
Not quite sure where I dropped the ball at here, or what is consuming large amounts or resources.
So instead of going back to the server to get fresh values, I just took them from the html in the option list of the dropdownlist, and parsed out the values I wanted.
Anyways, any insight would be appreciated.
$('[id*="_ddl_EstimateShipping_RateAPICode"]').change(function () {
var option_Textline = $(this).find(":selected").text();
if (option_Textline != "") {
var n = option_Textline.indexOf("--");
var option_RateName = option_Textline.substring(0, n).trim();
var option_RatePrice = option_Textline.substring(n + 2, option_Textline.length).trim();
option_RatePrice = parseFloat(option_RatePrice.replace(/[A-Za-z$-]/g, ""));
$('[id*="_lbl_Information_ShipRatePlan"]').text(option_RateName);
$('[id*="_lbl_Information_ShipRatePrice"]').text("$" + option_RatePrice);
var n_ProductTotals = $('[id*="_txt_Helper_ProductTotals_Field"]').val();
var o_PromoDiscount = $('[id*="_lbl_PromoCode"]').text();
var o_GCertificate = $('[id*="_lbl_SubTotals_GiftCertificates"]').text();
var o_SalesTax = $('[id*="_lbl_SubTotals_SalesTax"]').text();
o_PromoDiscount = parseFloat(o_PromoDiscount.replace(/\$/g, "").replace(/\-/g, ""));
o_GCertificate = parseFloat(o_GCertificate.replace(/\$/g, "").replace(/\-/g, ""));
o_SalesTax = parseFloat(o_SalesTax.replace(/\$/g, ""));
var n_SubTotal = parseFloat(n_ProductTotals - parseFloat(o_PromoDiscount));
var n_GrandTotal = parseFloat(parseFloat(n_SubTotal) + parseFloat(option_RatePrice) + parseFloat(o_SalesTax) - parseFloat(o_GCertificate));
if (!isNaN(n_SubTotal)) {
$('[id*="_lbl_DisplayShoppingCart_SubTotals_SubTotal"]').text("$" + n_SubTotal.toFixed(2));
}
if (!isNaN(option_RatePrice)) {
$('[id*="_lbl_SubTotals_Shipping"]').text("$" + option_RatePrice.toFixed(2));
}
if (!isNaN(n_GrandTotal)) {
$('[id*="_lbl_SubTotals_GrandTotal"]').text("$" + n_GrandTotal.toFixed(2));
}
}
});
return false;
}
|
|
|
|
|
Well after much diagnosing and more optimizing, the function I posted has nothing to due with the slowness.
Other dropdownlist are doing the same thing as well, even on other pages. Maybe I just need to reboot my computer an clean things up
|
|
|
|
|
I'm having trouble using each on this json. Normally I use
for ( i=0; i<=states_array_Len-1; i++) {
var stateName = states_array(i).states_Name
}
but in this case, each state name is different and I need the state code as well.
I'm thinking maybe I need a double $(each), I'm not sure.
On this sample, I get 0 in the dropdown list
my json
{
"exit_Code": 2,
"states_Count": 58,
"states_Array": [
{
"Choose State": "--",
"Alabama": "AL",
"Alaska": "AK",
"Arizona": "AZ",
my jquery
var objB = jQuery.parseJSON(responseText.d);
exitCode = objB.exit_Code;
var states_Count = objB.states_Count;
var states_Array = objB.states_Array;
var states_Array_Len = objB.states_Array.length;
if (exitCode == 0) {
$('[id*="_ddl_Modal_StateCode_Field"]').empty();
$.each(states_Array, function (key, value) {
$('[id*="_ddl_Modal_StateCode_Field"]').append($("<option ></option>").html(key).attr("value", value));
});
}
|
|
|
|
|
JavaScript's for loop does what you want:
for(var stateName in states_Array){
var stateShortCode = states_Array[statesName];
}
It seems like you have that dictionary backwards, the short code should be the key and the long name the value.
Also, in your sample, exit_Code is non-zero so the inner code won't ever run. That's probably all that's needed to make it run, as I imagine jQuery.each uses a for loop already.
|
|
|
|
|
I didn't think of your example.
I thought of this last night while bowling, and it worked. I'm kind of surprised it works, but it makes sense. I just had to get into the array object before using $(each). Must be that double $(each) I was thinking of yesterday, in which the first $(each) is the array, and the 2nd $(each) is the pair value.
for (i = 0; i <= states_Array_Len - 1; i++) {
$.each(states_Array[i], function (key, value) {
$('[id*="_ddl_Modal_StateCode_Field"]').append($("<option ></option>").html(key).attr("value", value));
});
}
|
|
|
|
|
I want to calculate the difference between two dates in Year, Month and Days Format. It should check the Leap Year also.
Eg: 01/02/2012 - 29/02/2012 should get result as 1 Month
01/02/2012 - 30/03/2012 - Result should be 1 Month and 30 Days.
|
|
|
|
|
|
my date is 29/02/2012 and i want to add 1 day to this date the answer should be 01/03/2012. how it is possible.I tried the following way, but not working.
var Dte="29/02/2012"
var today=new Date(Dte.substring(6,10),Dte.substring(3,5),Dte.substring(0,2));
today.setDate(today.getDate()+1);
|
|
|
|
|