-4

I am trying make an onclick function so when you click a div tag it will send you to a new page, similar to the A + href attribute in HTML. Anyone have any idea how to do this? Or do I just add a A tag to the div? IDK and I don't really have a clue.

ComFreek
  • 29,044
  • 18
  • 104
  • 156
  • Best idea would be to add a `a` tag yes. Try it. – putvande Sep 07 '13 at 18:38
  • We ask that you make an attempt to solve the problem before you ask it. What have you tried? – gunr2171 Sep 07 '13 at 18:39
  • If you don't have a clue, please get a clue before asking a question. Don't just ask, please do it for me! – Ruan Mendes Sep 07 '13 at 18:41
  • Basic does for navigation in a web page with JavaScript https://developer.mozilla.org/en-US/docs/Web/API/window.location or https://developer.mozilla.org/en-US/docs/Web/API/window.open – epascarello Sep 07 '13 at 18:41
  • @JuanMendes You once were a beginner at coding JavaScript. Not everyone knows everything. – epascarello Sep 07 '13 at 18:42
  • @epascarello: We all were beginners. But most of us also took the time to learn the basics for ourselves through personal research. Just because someone is a beginner doesn't mean they're entirely incapable. – user2736012 Sep 07 '13 at 18:44
  • Thanks Juan... I do have quite the experience with HTML, CSS, and Javascript the problem is I never really wanted to make a div clickable for link purposes that is. Thanks for being rude though... – user2752448 Sep 07 '13 at 18:45
  • @user2752448 At least I'm not being sarcastic, you have to at least show an attempt, otherwise, you're just asking others to write for you. Others seem to agree from the downvotes. – Ruan Mendes Sep 07 '13 at 18:49
  • @epascarello When I was a beginner, I looked for answers on my own, tinkered with code, didn't just ask others to figure out things for me. If I tried and it didn't work, then I would seek help. – Ruan Mendes Sep 07 '13 at 18:51
  • I did try multiple times using w3 schools and looking at questions in this format, but I guess the people here found much better help then me because anything I found seemed to WAY over my head to read with 100 lines of code that I didn't really feel like diagnosing through. – user2752448 Sep 07 '13 at 18:53
  • @user2752448 That's my point, you don't seem willing to do research, so I (and others) aren't willing to help until you do so. The more effort you put into a question, the more likely you are to get an answer. You didn't even mention what you've tried and what didn't work. You won't be looked at favorably in a professional environment if you just ask questions without showing due diligence http://mattgemmell.com/2008/12/08/what-have-you-tried/ – Ruan Mendes Sep 07 '13 at 19:07
  • What's the point of adding what didn't work? It obviously didn't work and if your going to give me and answer that's correct you don't need a wrong answer to my situation. So that's just dumb. – user2752448 Sep 07 '13 at 19:09

3 Answers3

1

Best way is just to make an a html tag and style it as a block element.

Another way is to use Javascript to handle onCLick event, but it's not worth to do it in your case.

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
0

Easy javascript onclick...

Add an event listener to the div to listen for a click event. When it detects a click, it willl execute the window.location.href statement. Just add the web address of where you want to go. :)

var link = document.getElementById("//whatever your div is called");

link.addEventListener("click", function() {

window.location.href="// put the web address here";

});

OR You could just add an tag before the div as @amdorra suggested, this works in HTML5 and most browsers now. :)

  • Wow, thank you tinkerbot, I really wanted to do this in Javascript to keep it my HTML code as clean as possible (and a few lines as possible) because I find it hard to find something because my HTML is always a mess after about one-hundred lines. Thanks A LOT! – user2752448 Sep 07 '13 at 18:48
  • hehe, I feel exactly the same, I like to keep my languages seperate! My pleasure, any more issues let me know! :P –  Sep 07 '13 at 18:48
0

I wouldn't recommend using onclick but if you are going to, try using location.href

<div onclick="location.href='http://foo.com';">bar</div>
Community
  • 1
  • 1
user2521439
  • 1,405
  • 2
  • 13
  • 19