JS Click event issue.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MadTogger
    New Member
    • Mar 2010
    • 2

    JS Click event issue.

    Hi,

    I have a download counter on my website, here, that I am trying to get to work correctly.

    The files for download are displayed in a 2 column table along with their respective download counts.

    Here is the code that displays the table and files on the page:

    Code:
    <center>
    <table>
    <tr>
    <td>File Name</td>
    <td>Downloads</td>
    </tr>
     <?php 
    
            foreach($files_array as $key=>$val)
            {
                echo '
    <tr>
    <td><a href="http://www.madtogger.co.uk/dlc_download.php?file='.urlencode($val).'">'.substr($val, 0, strpos($val, '.')).'</a></td>
    <td><span class="download-count">'.(int)$file_downloads[$val].'</span></td>
    </tr>';
            }
        
        ?>
    
    </table>
    </center>
    When the user clicks on a file it initiates the download process and updates the counter via a call to my js script, see below:-

    Code:
    $(document).ready(function(){
    	/* This code is executed after the DOM has been completely loaded */
    
    	$('tr').click(function(){
    		
    		var countSpan = $('.download-count',this);
    		countSpan.text( parseInt(countSpan.text())+1);
    	});
    });
    Now my problem is that the click event is on the <TR> tag of the table and even when a user clicks in cell2 of the table the counter will increase, although it does return to normal after a page refresh but obviously the file download will not start.

    I would really like to isolate the event so it only actually happens when cell1 where the actual filename is, is clicked.

    I have tried changing the event to happen on the <TD> tag, while this still activates the download, the counter will not increase and I still have the same problem in clicking on cell2 increases the counter without downloading.

    I hope all this makes some kind of sense and would appreciate any help at all.

    Regards

    K..,
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    as far as I can tell, the download is not triggered by JavaScript …

    if you put the increment event in the TD, you have to fetch the span from the next sibling. jQuery certainly has an accessor for that.

    Comment

    • MadTogger
      New Member
      • Mar 2010
      • 2

      #3
      Thanks for the reply.

      I am pretty new at all this but I do understand that the download is not triggered by the js file, that seems to be only used for updating the counter.

      How would you code it to resolve the issue I am having?

      An example would probably suffice for me to then work through it.

      Regards..,

      K

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I don’t know because I don’t use jQuery. you have to search in the documentation or ask someone good at jQuery.

        Comment

        Working...