0

I am trying to get the href value passed to a variable when the user clicks on the link from a particular class. This is what I have right now.

$(document).on('click',"a.show-notes",function{
    var $url = $(this).attr("href");
)};

This is not working for me though, and no value is being passed to the variable. I have also tried something like this, with no luck...

$("a.show-notes").on('click',function(){
    var $url = $(this).attr("href");
)};

I imagine that there is something obvious here that I am missing, but I've been starring at it for so long that my mind is fried.

5
  • The first code is a syntax error, the second should work as long as the element is static. Where are you trying to use this variable ? Commented May 8, 2014 at 20:36
  • Could you please try ".show-notes a" it will fix the code I think... Commented May 8, 2014 at 20:36
  • 1
    You're declaring "$url" inside the function, so it's local to that function. It'll be un-seeable outside the function. Commented May 8, 2014 at 20:36
  • Okay, there must be something else going on here. Does attr("href") return what is hard coded? My href is being generated by an embedded ruby variable. I wasn't thinking that this could be the issue though, because when I check the source code or inspect the link in my browser, I am seeing the desired href, of course. Commented May 8, 2014 at 20:59
  • Thanks for all the comments everybody. I'll come back to it tomorrow. Commented May 8, 2014 at 21:00

3 Answers 3

3

Ensure you're setting those hooks after the DOM has loaded and those elements exist. Wrap them inside a $(document).ready function like this:

$(document).ready(function() {
  $("a.show-notes").on('click',function(){
    var url = $(this).attr("href");
    console.log(url);
  });
});
Sign up to request clarification or add additional context in comments.

5 Comments

This should work! One more thing you can try is testing the window.location.hash variable, which returns the hash-component of the current url.
I could be wrong but using .on should make the document.ready irrelevant.
The first version should work without doing this though
From jQuery spec: "Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on()" api.jquery.com/on so the delegated format (first alternative) would work without dom ready
In the first version, the handler is bound to document (just like the ready handler in your code)
1

You have some syntax errors:

$(document).on('click',"a.show-notes",function(){
    var $url = $(this).attr("href");         //^missing parenthesis
    alert($url);
});
//^bad closing

Note: Also the $variable name convention is normally used only for jquery elements like:

var $element = $("li");

Comments

0

Try

$('a.show-notes').click(function(){
    var $url = $(this).attr('href');
});

The variable $url variable can be named however you like (url, myLink, etc.) but it is only visible inside the click() function. If you want to use it outside, create the variable before the click() function, and set it inside.

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.