[help] onclick event in javascript

Discussion in 'JavaScript' started by News Updates, Mar 2, 2012.

  1. #1
    hello friends, I need a little help.. here is my code:

    1st - <a href="http://mysite.com/search" id="search">search</a>

    I want to make hyperlink using 2nd method but its not working please let me know what I'm doing wrong.

    the 1st is another link in my template. I want to make a link using 2nd method at another place.

    2nd <a href="#" onclick="window.location.href=$("#search").attr('href')">search</a>
     
    Solved! View solution.
    News Updates, Mar 2, 2012 IP
  2. #2
    The problem is that you used double quotes inside double quotes so your onclick command is actually just "window.location.href=$(".

    This should work:
    <a href="#" onclick="location.href=$('#search').attr('href')">search</a>

    or you can cancel the inner double quotes:

    <a href="#" onclick="location.href=$(\"#search\").attr('href')">search</a>
     
    Arttu, Mar 2, 2012 IP