-2

In javascript I want to tell post-container that when I click on it, take the href located at link and go to page X.

There are multiple post-container

My code is this:

<div class="post-container">
  <a class="link" href="X">
    <h2 class="post-title">Hello, world!</h2>
  </a>
  <div class="date-posts">...</div>
</div>

enter image description here

I want to be able to click outside the Hello, world! title of my post in any area of ​​post-container and go to page X

Any idea how to do this?

Sebastián Varella Gmz
  • 1,395
  • 2
  • 7
  • 12

5 Answers5

3

I hope I'm interpreting your question correctly, but you can just rearrange where you're writing your a href tag. Instead, wrap your div inside of it. This will make the entire div element a link.

<a class="link" href="google.com">
    <div class="post-container">
        <h2 class="post-title">Hello, World!</h2>
        <div class="date-posts"></div>
    </div>
</a>
paoiherpoais
  • 314
  • 1
  • 10
  • I did what you suggest, but it doesn't work in my case. I share a question I asked so you can better understand my problem. https://stackoverflow.com/questions/58295443/post-clickable-in-blogger – Sebastián Varella Gmz Oct 09 '19 at 17:02
  • Sebastián Varella Gmz It would work if you managed to do it. The other question is something similar but different, the answer has other considerations. You might want to add a JavaScript onclick event to the div and forget the a, like this `
    `
    – Tiago Martins Peres 李大仁 Oct 09 '19 at 17:12
1

This will work.Add Event listener to the parent element (Event Delegation).You can take the href property of the child element and then use window.location.href.You might need to add preventDefault() to avoid default behaviour of a tag

If you have multiple tags with same class name.You have to use querySelectorAll .Either way if you have single elements or multiple elements with same class name use querySelctorAll.

// if you have one tag
let ele = document.querySelectorAll(".post-container")
ele.forEach(element => {
  element.addEventListener("click", function(e) {
    e.preventDefault();
    let href = element.children[0].getAttribute("href")
    window.location.href = href;

  })

})
.post-container {
  background-color: green;
}
<div class="post-container">
  <a class="link" href="X">
    <h2 class="post-title">Hello, world!</h2>
  </a>
  <div class="date-posts">...</div>
</div>
Shubh
  • 7,299
  • 3
  • 15
  • 37
0

Javascript:

var elements = document.getElementsByClassName('post-container');

var loadLink = function() {

    var link = this.getElementsByTagName("a")[0].href;

    window.location.href = link;

};

for (var i = 0; i < elements.length; i++) {
    elements[i].addEventListener('click', loadLink, false);
}

Check working demo

Āchahms
  • 351
  • 4
  • 11
0

Assuming you have multiple .post-containers you would need to iterate over each of them and add an event listener to handle click events. On click, find the a get its href and set the window.location (I just console log it in this demo)

(function(){
    let elems = document.querySelectorAll(".post-container");
    for (i = 0; i < elems.length; ++i) {
        let el = elems[i];
        el.addEventListener("click", function(e) {
            e.preventDefault();
            let href = el.querySelector("a").getAttribute("href");
            console.log("href",href);
            //window.location.href = href;
        })
    }
})();
<div class="post-container">
  <a class="link" href="X">
    <h2 class="post-title">Hello, world!</h2>
  </a>
  <div class="date-posts">...</div>
</div>
<div class="post-container">
  <a class="link" href="Y">
    <h2 class="post-title">Goodbye world!</h2>
  </a>
  <div class="date-posts">...</div>
</div>
Moob
  • 13,593
  • 1
  • 29
  • 45
  • I get the next error message: The content of elements must consist of well-formed character data or markup. In the column 20 of line `for (i = 0; i < elems.length; ++i) {` Please check this link for understand my problem: https://stackoverflow.com/questions/58295443/post-clickable-in-blogger – Sebastián Varella Gmz Oct 09 '19 at 17:50
-1
document.getElementsByClassName('post-container')[0].onclick = function(e){
  location.href = this.children[0].href;
}