views:

38

answers:

2

i have the table of rows which are dynamic in nature from server side script (PHP) so each and every first td in the row of every table will have the context menu (jquery plugin) applied .

How do i get the attribute id(which is dynamic in nature) of when ever the first item of context menu is clicked

 $("#mytable td:first-child").contextMenu("myMenu1",{
      bindings: {
            'hdr': function(t) {
                  alert($(this).closest("td").find("a").attr("id"));

             }
      },
      menuStyle: {
            border: '1px solid #000'
       }
      });

But my problem from the above code it is returning same id called "test0" for each and every row . But my also goal is to get the value of data-value1 (dynamic) for a href tag on each row when ever the first menu item is clicked in the contextmenu

+2  A: 

According to the docs:

bindings An object containing "id":function pairs. The supplied function is the action to be performed when the associated item is clicked. The element that triggered the current menu is passed to this handler as the first parameter.

So you have a parameter t that should refer to the element you clicked in order to get the context menu.

Try this:

alert( $( t ).closest("td").find("a").attr("id") );
patrick dw
@patrickdw: the above statement has helped me to solve it How do i get the data-value of that ,< a href >
Someone
@Someone - Are you saying you want the `href` attribute value? Just do `.attr("href")` instead of `.attr("id")`. Is that what you meant?
patrick dw
@patrick: i want to get the data-value attribute of the <a href>
Someone
@Someone - What is a "data-value attribute"?
patrick dw
@Patrick: data-value attribute is HTML5 attribute Please refer this link http://www.javascriptkit.com/dhtmltutors/customattributes.shtml <a href="abc.php" id ="Test0" data-city="$row['SOmeColumn Value']" data-State="$row['SOmeColumn Value']" data-country="$row['SOmeColumn Value']">i have said in this manner but it did not work alert( $( t ).closest("td").find("a").attr("data-city") );
Someone
@Someone - Not sure. If you do `alert( $( t ).closest("td").find("a").length );`, what do you get?
patrick dw
A: 

If it's the first element of a <tr>, for example, you could do something like this:

var yourID = $("tr.yourClass td:first").attr("id")

Alex Mcp