1

I am new in jQuery ,I am trying to get the value of href attribute when the user clicks on a link but I am getting same values for href attributes. here is my html code

<div class="container">
    <ul>
        <li><a href="#1" >Sign In</a>
        </li>
        <li><a href="#2">Brand</a>
        </li>
        <li><a href="#3">Distributor Brand</a>
        </li>
        <li class="language"> <a href="#4">English</a>

            <ul id="language-select">
                <li><a href="#5">Spanish</a>
                </li>
                <li><a href="#6">French</a>
                </li>
                <li><a href="#">German</a>
                </li>
            </ul>
        </li>
    </ul>
</div>

and js

$(document).ready(function(){    
    $("a").click(function(){            
        var value=$(".container").find("a").attr("href");            
        alert("hello------>  "+value);
    });
});

Here's the fiddle http://jsfiddle.net/suraj0750/rmbvo9oh/1/

Rohan Kumar
  • 39,838
  • 11
  • 73
  • 103

2 Answers2

7

try this :-

 var  value=$(this).attr("href");

Demo

Mohit Kumar
  • 9,065
  • 2
  • 27
  • 43
3

In JavaScript, when you bind an event to an element, the this variable is being available (of course depending on the element and event). So, when you click an element and bind a function, within that function scope the clicked element is referenced as this. Writing $(this) you make a jQuery object out of it:

$(document).ready(function(){

  $("a").click(function(){

    var value = $(this).attr("href");

    alert("hello------>  " + value);
  });


});
Alex
  • 9,486
  • 2
  • 30
  • 48