Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link to a <div> on another page?

Tags:

html

I'd like that a specific link goes to a certain header on another page. I know how to do it on the current page.

like image 253
online Thomas Avatar asked Dec 13 '13 11:12

online Thomas


People also ask

How do I link to another page in a div?

You need to use the href attribute to link to another page. The value of the href attribute is usually a URL pointing to a web page (like the one above). You can also link another HTML element or a protocol (for example, sending email), and you can execute JavaScript using the href attribute.

How do you make a div a link?

If you absolutely need to use JavaScript, one way is to find a link inside the div and go to its href when the div is clicked. This is with jQuery: $(". myBox").

How do I link to an element on the same page?

Linking to an element on the same page Note: You can use href="#top" or the empty fragment ( href="#" ) to link to the top of the current page, as defined in the HTML specification.


2 Answers

Take a look at anchor tags. You can create an anchor with

<div id="anchor-name">Heading Text</div> 

and refer to it later with

<a href="http://server/page.html#anchor-name">Link text</a> 
like image 143
kfb Avatar answered Oct 14 '22 23:10

kfb


You simply combine the ideas of a link to another page, as with href=foo.html, and a link to an element on the same page, as with href=#bar, so that the fragment like #bar is written immediately after the URL that refers to another page:

<a href="foo.html#bar">Some nice link text</a> 

The target is specified the same was as when linking inside one page, e.g.

<div id="bar"> <h2>Some heading</h2> Some content </div> 

or (if you really want to link specifically to a heading only)

<h2 id="bar">Some heading</h2> 
like image 34
Jukka K. Korpela Avatar answered Oct 15 '22 00:10

Jukka K. Korpela