Search code examples
htmlparentsiblings

Referring to sibling of parent div in HTML


I have two div elements:

<div id="one">
    <a href="#two">Link to two</a>
</div>
<div id="two">
    Link to me
</div>

This causes the following when I click on the link with href attribute being #two:

http://domain.com/one#two

but I want:

http://domain.com/#two

Is this possible without using javascript?


Solution

  • Solution

    Considering the strange behavior of your page, NO, it's not possible without JavaScript.

    On click event of the links, go to the location specified in the href:

    Javascript

    window.onload = function(){
        /* On window load event, we loop through each link */
        var a  = document.getElementsByTagName('a');
        for (var elem in a)
        {
            /* If it's a DOMElement */
            if(a[elem].nodeType ==1)
            {
                /* We bind the window.location.href event, sending it to the value of the href attribute of the selected anchor element. */
    
                a[elem].addEventListener("click",function(e){window.location.href = this.getAttribute('href');alert(window.location.href);});
    
                /* console.log(window.location.href); */
            }
        }
    }
    

    -->Live Demo<--