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

4
  • are you using the same id on all the links?...as what i see in the code above? Commented May 12, 2010 at 13:16
  • I just pasted your code into an old jQuery test page of mine, and it works fine. I can't see anything wrong. Have you tried just this code in a single new test page, as an experiment? Commented May 12, 2010 at 13:26
  • What browser are you using and what is your jQuery version? Commented May 12, 2010 at 13:29
  • @Piotr 1.4.1 and Firefox. @Matt I have used the same code I pasted on lots of other pages making me wonder whats different about this one? Commented May 12, 2010 at 13:44

5 Answers 5

5

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() {
Sign up to request clarification or add additional context in comments.

7 Comments

@Chris - Updated with some more scenarios to check, see if any apply.
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 - have an example page I can take a peek at?
@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 - Are the elements added via ajax or anything?
|
2

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.

Comments

2

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

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

1 Comment

return false already prevents the default behavior and any bubbling.
0

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! ^^

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.