tags:

views:

115

answers:

4
 var value;
    $("#Grid1").click(function(e) {
            var row = jQuery(e.target).parent();
            value= row.attr("id");
        });
        var onrowclick = function() {
        ("#Grid1").click($("#showgrid").load('/Names/Friends/satish/' +  value));
    };

i am trying to send the value on URL.. when I am giving like this I am not getting the output result... am I doing this correctly?

is the problem that I am handling click event on same grid two times?

A: 

what do you need to do?

var value;

$("#rowid").click(function() {
    value = $(this).value();
});

or you can try something like this:

var value;

$("#rowid").click(function() {
    value = $(this).value();
    $("#showgrid").html( $.load("/Names/Friends/Satish/" + value) );
});
GerManson
Thanks,but I want this var onrowclick.. variable bec I am assiging this on server side Rowclick evnt...thanks
kumar
Using this method (based on ID), you would need to manually assign a click event handler to each individual row. kumar appears to have the handler assigned to a container of the rows, and is relying on bubbling to fire the code. It is a good and efficient method if you have lots of rows, or rows being frequently added and removed.
patrick dw
+2  A: 

Hard to say without seeing HTML, but I think you're trying to do something like this:

// Initialize variables outside of the handler
var row;
var value;

$("#Grid1").click(function(e) {
    // Assign values to variables when event handler fires
    row = jQuery(e.target).closest('tr');
    value= row.attr("id");
    $("#showgrid").load('/Names/Friends/satish/' +  value);
});

// Now you can access the local variables

function someFunction() {
    alert(value);
    alert(row.get(0));
}

I changed parent() to closest() since I don't know your HTML structure.


EDIT:

For clarity, I am assuming that #Grid1 is some sort of container (table perhaps), and you are clicking on something in the content of a tr. And #showgrid is a separate component external to the container that displays additional info.

EDIT:

I'll assume from your comment that you need to access the row element and/or ID from outside the handler. I've updated the example to reflect how this can be done. Let me know if that's not what you meant.

patrick dw
Yes your perfect..while display ShowGrid I am passing first grid row id value to the Actionresult method.. to get that perticular users..thanks
kumar
You're welcome.
patrick dw
Can i write this to get the row value in to var var value = $("#Grid1").click(function(e) { var row = jQuery(e.target).parent(); value= row.attr("id"); }); I need to get the Row value in value? thanks
kumar
I don't understand what you mean. The way it is, you have both the row and the value of its ID stored in variables. Are you saying that you need to access the value outside of the function?
patrick dw
I've updated my answer. I think you need to explain specifically what you want to do. Just remember, `$('myElement').click(funct...` does **not** fire the code. It merely sets it up to fire when the user clicks on the element.
patrick dw
A: 

Why not use the following?

$("#Grid1").click(function(e) {
         var value= $(e.target).parent().attr("id");
         $("#Grid1").load('/Names/Friends/satish/' +  value);
     });
};

This code is still fragile, but probably does more of what you want.

DDaviesBrackett
A: 

UPDATED: in responce to your comment:

if you want assign a variable outside the click event you can do this:

$(function () {
  // declare the variable.. this one will hold the parent id
  var my_value;
    // now the click event...
    $("#Grid1").click(function(e) {
             e.preventDefault();
             // my_value now is setted outside...
             my_value = $(this).parent().attr("id");
         });

  // now for the example we observe the click event you can use it in other way!

    $('#Grid1').bind( 'click' , function() { 
      // now as you can see we are using my_value that come from outside!
     $('#showgrid').load('/Names/Friends/satish/' + my_value );

    });

});

ADDITIONAL NOTE

now i don't know if the #Grid1 is inside the #showgrid but if this is the case, you need to use the live() method!

like this:

$("#Grid1").live( 'click' , function(e) {
// do something here as above...!
});

assuming:

<div id="this_is_parent_of_Grid1">
<a id="Grid1" href="">click</a>
</div>

OR if you use table

<table><tr>
<td id="this_is_parent_of_Grid1"><a id="Grid1">click</a></td>
</tr></table>
aSeptik
Can i write this to get the row value in to varvar value = $("#Grid1").click(function(e) { var row = jQuery(e.target).parent(); value= row.attr("id"); });I need to get the Row value in value?thanks
kumar
by using **e.preventDefault();** let you click two times the same grid!
aSeptik