Binding Events rather than using onclick

Posted by Abs on Stack Overflow See other posts from Stack Overflow or by Abs
Published on 2010-06-13T20:13:20Z Indexed on 2010/06/13 20:22 UTC
Read the original article Hit count: 163

Filed under:
|

Hello all,

I have been told its better to bind events to elements rather than having onclicks with functions everywhere. I agree with this as it devolves more from the HTML which means cleaner and easier to debug code.

But I am having trouble doing this! I loop through some data with PHP (see below) and I currently use onclick to call a function. How can I bind this onclick event to many elements using JQuery.

<?php foreach($main_tags as $key=>$value){ ?>
<li>
    <a id="<?php echo $key; ?>" href="#" onclick="tag_search('<?php echo $key; ?>'); return false;">
    <?php echo $key; ?>
        <span class="num-active">
            <?php echo $value; ?>
        </span>
    </a>
</li>
<?php } ?> 

Thanks all for any help

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery