tags:

views:

66

answers:

5

I want to perform some action when a link is clicked, I am using the following code to achieve this however it rarely works. If I click the link it usually refreshes the page, and 1/10 times it'll actually pop up "Hi". What is wrong?

$(document).ready(function()
{
    $('#slconfiglink').click(function()
    {
        alert("hi");
        return false;
    });

});

HTML:

<ul>
    <li><a href="#" id="slconfiglink">Config 1</a></li>
</ul>

Note that the ID's are not replicated elsewhere in the HTML

+3  A: 

Make sure to give your <a> a valid href, like this:

<a href="#" id="slconfiglink">Config 1</a>

Your anchor needs to have an href or a name to be valid, otherwise you'll get some funny behavior that'll vary by the browser used.

Also, unless you need the anchor for hover CSS, etc...you have the option to put the click handler right on the <li>, like this:

<ul>
  <li id="slconfiglink">Config 1</li>
</ul>

A few other things to check, make sure that ID is unique, if it's not use a class, like class="slconfiglink" and your selector would be ".slconfiglink" instead of "#slconfiglink".

If your elements are being added on the fly, change from

$('#slconfiglink').click(function() {

To use .live() like this (or with the class selector, if the multiple comment applies):

$('#slconfiglink').live('click', function() {
Nick Craver
Done this with no effect
Chris
@Chris - Updated with some more scenarios to check, see if any apply.
Nick Craver
Thanks Nick, very comprehensive answer however I'm still having no joy. I moved the ID to the `<li>` but this didn't work either. The HTML is static so no need for `live()` either. I'm totally confused as to why it's not working!
Chris
@Chris - are you using any other libraries?...
Reigel
@Chris - have an example page I can take a peek at?
Nick Craver
@Nick - I changed my code to use `live()` instead of `$(document).ready()` and this works... I wonder if, for some reason, `$(document).ready()` is having issues on this particular page?
Chris
@Chris - Are the elements added via ajax or anything?
Nick Craver
Nope, which is weird... I cleaned up all other Javascript stuff on the page and it made no difference... Either way `live()` works and I'm happy with that
Chris
@Chris - very weird indeed, if you get time, I'd run the page through a validator, unclosed tags, etc can throw `document.ready` for a loop: http://validator.w3.org/
Nick Craver
A: 

Is the element being created dynamically? If you are adding it using Javascript, you'll need to reattach your event handler after it's been added to the DOM.

DavidYell
No its just pure HTML
Chris
+1  A: 

Use the preventDefault() method to make sure the browser doesnt follow the link.

$(document).ready(function()
{
    $('#slconfiglink').click(function(e) {
        e.preventDefault();
        alert("hi");
    });
});
Fabian
`return false` already prevents the default behavior and any bubbling.
Nick Craver
I actually used this elsewhere so thanks
Chris
A: 

I had the same problem yesterday.
To solve I've just used simple javascript in combination with jQuery.

That's what I've done:

<script>
    function yourFunction()
    {
        alert("hi");
        //$("#slconfiglink").doWhatYouWant();
    }
    $(document).ready(function()
    {
        $("#slconfiglink").attr('onclick', 'yourFunction();')
           //I did it in this way to garantee the function 
           //will only be called after the DOM is ready
    });
</script>

<a id="slconfiglink" href="javascript:;">click me</a>

I hope this could be useful! ^^

Jayme
A: 

could you try changing the href of anchor tag to 'javascript:void(0);'

Sundararajan S