2

I am making a ASP.NET 3 MVC project, but it seems i can't make jQuery work properly.

In the view i have something like this:

<a id="login">lala</a>

Then in the .js file:

$("#login").click(function () {
    alert("lal");
});

The alert doesn't show on the screen. I've added the reference of my custom .js file in _Layout and updated to the newest library, but whatever I do I can't find any element. What am I doing wrong here :s

2
  • Is this wrapped in a $(document).ready() handler? If not, then your event won't work because it's trying to bind it before the DOM finished loading. Commented Apr 27, 2012 at 22:28
  • Assuming this is in the ready handler, this code works for me in jsfiddle using chrome: jsfiddle.net/4UkAg. You may want to add the href="#" attribute, otherwise the link won't appear clickable. Commented Apr 27, 2012 at 22:29

1 Answer 1

3

Try:

$(document).ready(function(){
    $("#login").click(function () {
        alert("lal");
    });
});

If this does solve your issue, google search for jquery document ready and read up on the tons of material already written out there on what this does and why it exists.

Sign up to request clarification or add additional context in comments.

7 Comments

I've tried wrapping it in .ready, but it didn't work. Is it possible that there's something specific about ASP.NET MVC 3 that I didn't notice?
It's very unlikely. I work exclusively with MVC3 and I have never run into something like this. Paste your exact code into a JSFiddle and let us check it out.
That jsfiddle will never work 'cause you're referencing mootools. you need to reference jquery, e.g. jsfiddle.net/C5rS9/7
considering that it works in jsfiddle when you reference jquery correctly, is there any chance you're loading the login panel asynchronously, after the $(document).ready() function runs?
I don't know. How can i check how the page is loaded.
|

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.