Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    96
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Help getting a variable out created in Javascript

    Hi,

    I've managed to get myself in a right pickle over getting a variable out of a table I've created using Javascript.

    Basically, the table is within a basket system and works in that when you click on a row, the price in that row is set for the product price. Here is the table and preceeding Javascript:
    Code:
    <script type="text/javascript">
    function setEvent(){	
    var ids = [1];
    for(var j=0;j<ids.length;j++){
    var rows=document.getElementById(ids[j]).getElementsByTagName('tr'), i=1, r;
    while(r=rows[i++]){
    i<rows.length?r.onclick=doActions:null;
    }
    }
    function doActions(){
    var rows=this.parentNode.getElementsByTagName('tr'), i=1, r, c, v;
    var th=this.parentNode.parentNode.getElementsByTagName('th');
    while(r=rows[i++]){
    if(r==this){
    r.style.backgroundColor='#ccc';
    c=this.getElementsByTagName('td');
    v=c[c.length-1].firstChild.data;
    th[th.length-1].getElementsByTagName('p')[0].firstChild.data=v;
    }
    else{r.style.backgroundColor=''}
    }
    }
    }
    onload=setEvent;
    </script>     
    
    <table cellpadding="3" cellspacing="1" class="basket_options_table" id="1">
    	<tr>
    		<tr>
                 <th>Usage</th>
                 <th>Resolution</th>
                 <th>File Size</th>
                 <th>Price</th>
            </tr>                                       
       </tr>
       
       <tr class="normal" onmouseover="this.className='normalon'" onmouseout="this.className='normal'" >
                  <td>FREE</td>   
                  <td>320 x 288 pixels</td>   
                  <td>size</td>   
                  <td>&pound;0</td>   
       </tr>
       <tr class="normal" onmouseover="this.className='normalon'" onmouseout="this.className='normal'" >
                  <td>Personal Use</td>   
                  <td>320 x 288 pixels</td>   
                  <td>size</td>   
                  <td>&pound;5</td>   
       </tr>
       <tr>
                  <th colspan="3"></th>
                  <th class="basket_record_price">
                  <p>&pound;</p>
                  </th>
       </tr>
    </table>
    How can I get the price out which is dynamically created in the script? I'm really confused and rather new at this so any help would be great!
    Last edited by Kor; 03-21-2009 at 09:54 AM. Reason: wrap the code [code][/code]

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    I don't pretend to understand the question, and I'd bet nobody else does either.

    For the life of me, I can't see how that code is supposed to do anything. It certainly doesn't seem to work. If you click on the top row, all you get is an error (missing a child node) and if you click on any other row, nothing happens.

    If all you wanted to do was copy either the zero or the 5 to the last line when one of the two data rows was clicked on, you couldn't have made it much harder, I think.

    Something as simple as this will accomplish that:
    Code:
    <table cellpadding="3" cellspacing="1">
    <tr style="background-color: black; color: white;">
    	<th>Usage</th>
    	<th>Resolution</th>
    	<th>File Size</th>
    	<th>Price</th>
    </tr>
    <tr class="normal" 
        onmouseover="this.style.backgroundColor='lightgrey';" 
        onmouseout= "this.style.backgroundColor='transparent';" 
        onclick="document.getElementById('TOTAL').innerHTML='0';">
    	<td>FREE</td>
    	<td>320 x 288 pixels</td>
    	<td>size</td>
    	<td>&pound;0</td>
    </tr>
    <tr class="normal" 
        onmouseover="this.style.backgroundColor='lightgrey';" 
        onmouseout= "this.style.backgroundColor='transparent';" 
        onclick="document.getElementById('TOTAL').innerHTML='5';">
    	<td>Personal Use</td>
    	<td>320 x 288 pixels</td>
    	<td>size</td>
    	<td>&pound;5</td>
    </tr>
    <tr>
    	<td colspan="3"></td>
    	<td style="font-weight: bold;">&pound;<span ID="TOTAL"></span></td>
    </tr>
    </table>
    But since I'm not really sure what your aim was, the above could be completely inappropriate.

    By the way, numeric IDs--indeed, IDs just starting with a digit--are poison in some code.

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    ids must begin with a letter, not a digit or a special character. See: http://www.w3.org/TR/html4/types.html#type-id
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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