SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jquery return value

    I have a form, and in that form I have a div that list a table of pet owners.
    This table is loaded from a file called findowner.php.
    After clicking on the row of a pet owner I want to use the variable I got $(this).attr('rowid'). This is the owners id. In the php file I get it like this:
    Code:
    Other td elements here for name, address, etc.
    '<td>'.'<a id="tableid" href="#" rowid='.$rowid.' >'.$rowid.'</a>'.'</td>'.
    Code:
    $('a#tableid').click(function() {
    var vpetowner = $(this).attr('rowid');
    alert(vpetowner);
    $('#petowner').val(vpetowner);
    return false;
    });
    I can't seem to get the val(vpetowner) into the forms $('#petowner').
    If I load the findowner by itself (for testing), the alert(vpetowner); line works. But if clicked from the div where loaded from the other file (editpets.php), it doesn't alert and the $('#petowner').val(vpetowner); doesn't update.
    How would I properly pass this from the div to parent?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    This is where a demo testing page that we can look at too, and apply our debugging techniques to, will be of great help for us to help you.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am new to jquery, and there seems like it has some limits on passing values.

    How about a simple javascript example that if I could see an explaination of how it would be done in jquery:

    Say I have a page with a form that opens a modal popup window and list a table of values. This is real routine in the html/php/javascript world:

    Code fragment from child window that has an onclick attached to the tr.
    Code:
    if ($powners_Row = $db->fetch($powners_Res))
    	{
    		$kount = 0;
    		do
    		{
    			$kount = $kount + 1;
    			$rowid = "a".$kount;
    
    			$vid = $powners_Row['ownerid'];
    			echo
    				'<tr id='.$rowid.' onMouseover=this.bgColor="lightgrey" onMouseout=this.bgColor="#FFFFFF" onclick="cell(this.id)">'.
    				
    					'<td>'.$powners_Row['ownerid'].'</td>'.
    					'<td>'.$powners_Row['oname'].'</td>'.
    					'<td>'.$powners_Row['ostreet'].'</td>'.
    					'<td>'.'<a href="javascript:void(0)" onclick="">'.$rowid.'</a>'.'</td>'.
    				'</tr>';
    		}
    		while ($powners_Row = $db->fetch($powners_Res));
    	}
    	echo '</table>';
    NOTE THE
    Code:
    onclick="cell(this.id)
    Now the function that handles this:
    Code:
    function cell(id)
    {
      var x=document.getElementById('myTable').rows[id].cells;
      opener.document.peditform.ownerid.value = x[0].firstChild.nodeValue;
      opener.document.peditform.petowner.value = x[1.firstChild.nodeValue;
      opener.document.peditform.ostreet.value = x[2].firstChild.nodeValue;
      self.close();
    }
    This code works perfect.

    OK, the questions.
    1) How to add a onclick to a <tr> in jquery? Note each row has unique id.
    2) How to send the three values obtained back to parent?

    I don't use this technique now, I used to, I now return the ownerid from the table row, then do a lookup through ajax to get the rest of the data. Much like dhtmlgoodies.com's dynamic client lookup example. I also use the SACK library.

    I only put this popup window example here to understand converting from javascript to jquery, and how to go from either a child window to parent, or from a division to a field on the form. But really this is very basic stuff in regular javascript and ajax.

    For Dynamic client lookup on left click ajax scripts, then scroll down.
    http://www.dhtmlgoodies.com/
    Last edited by jim9; Nov 25, 2010 at 22:34. Reason: spell


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •