1

CODE:

<style>
    .mydiv {
        border: 1px solid #00f;
        cursor: pointer; 
    }
</style>

<div class="mydiv">blah blah blah. 
    <a href="http://examples.com">Examples link</a>
</div>


I want to click on 'blah blah blah' text and jump to the 'http://examples.com' link using jQuery.

2
  • thanks for replies..but i want the <a> tag inside the div tag Commented Dec 25, 2013 at 10:02
  • i can not hardcode the link for window.location.href in js script, as it can be changed on HTML page reload Commented Dec 25, 2013 at 10:21

7 Answers 7

7

JS:

$(".mydiv").click(function(){
    if($(this).find("a").length){
        window.location.href = $(this).find("a:first").attr("href");
    }
});

OR:

Simply write:

<a href="http://examples.com">
    <div class="mydiv">blah blah blah. 
        Examples link
    </div>
</a>
Sign up to request clarification or add additional context in comments.

6 Comments

op told that he want a tag inside div tag
@dholakiyaankit question has been updated after I posted answer. anyways I will update it.
Yes i know but i just informed you which will definitely leads op to answer +1 from me
@Hiral- i can not hardcode the link for window.location.href in js script, as it can be changed on HTML page reload
@user3113862 so you want that on clicking .mydiv, it should be redirected to any link within it. is it so??
|
0

Move the anchor element out of the div to wrap it

<a href="http://examples.com">
    <div class="mydiv">blah blah blah</div>. 
    Examples link
</a>

Comments

0
$(document).on("click", ".mydiv", function(){
    location.href = "http://examples.com";
}

Comments

0

If you want to use jQuery to do this, you're looking for the click function.

$('.mydiv').click(function(){
    location.href = 'example.com';
}); 

Comments

0

With JQuery as was requested:

$(document).ready(function() {
  $('.mydiv').click(function() {
   document.location.href='http://examples.com';
  });
});

OFC, you can (and should) let the browser handle it as suggested by Hiral.

Comments

0

In consideration of the other answers which haven't yet been accepted, I assume that you want to use multiple <div class="mydiv">, with differing href attributes of the <a> element.

If the <a> element will always be the last child of its parent:

$(".mydiv").click(function(){
    window.location.href = this.lastChild.href;
});

Remember to remove whitespace and the end of the <div>.

JSFiddle: http://jsfiddle.net/JAJuE/1/


If the <a> element will be somewhere (though not necessarily at the end) in the <div>:

HTML

<div class="mydiv">blah blah blah.
    <a href="http://examples.com" class="mylink">Examples link</a>
</div>

JS

$(".mydiv").click(function(){
    window.location.href = this.getElementsByClassName("mylink")[0].href;
});

JSFiddle: http://jsfiddle.net/JAJuE/

Comments

0

you can simply do with HTML

<a href="http://examples.com">
   <div class="yourDiv">blah blah blah. 
      Examples link
   </div>
</a>

but

if you want to do that with jquery do something like this

$(document).ready(function(){

    $(".yourDiv").on('click', function(){
        //As an HTTP redirect (back button will not work )
        window.location.replace("http://www.example.com");
    });

});

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.