How do I not affect the <body> with a JS function and take the value of the required class only?

Asked

Viewed 120 times

1

index.html

<body class="teste">
<h1 id="teste-zica">teste</h1>
<h2>teste2</h2>
<h3>teste3</h3>

js.js

addEventListener("mouseover", paradaDeSucesso);
function paradaDeSucesso(event) {
    $(event.target).addClass("intro");
    this.addEventListener("click", function(){
        var x = document.getElementsByClassName("intro")[0].textContent;
        $(event.target).removeClass("intro");
    });
}

The problem is this: every time mine hover Ta taking the body, and so when I click he picks up the whole amount, it wasn’t supposed to happen this, there’s a way to lock the addClass for the tag body?

PS: What I want to do is to let the location the mouse is with the tag and as soon as it is clicked take the content of that Class, "intro" in the case, to use in another function to translate to pounds the content that was clicked.

  • I do not see in what case it will be useful to use such a comprehensive event, but test event.stopPropagation(); to prevent the event from recurring body, but without explaining better what you want to do is difficult to help.

  • @Sergio I am developing a translation API from pt-br to pounds, in my view I have to have a class/id to identify the content to be caught by the system to make the translation.

  • Okay, in that case you better put that addEventListener to each element. You can give an example of HTML?

  • I confess I don’t quite understand what you want to do.. can you create a code example to make it clearer? It can be here or on https://jsfiddle.net/

  • @Ricardopunctual https://jsfiddle.net/tkssyvme/ here it is, sorry for the delay, in case the class is going pro body and also html, I would like to not pass because in case the user click will get all the text of the page.

  • @Sergio https://jsfiddle.net/tkssyvme/ here is the example, when the mouse cursor is somewhere other than the Divs and etc it ends up being on top of the body and so picks up adds the class "Giulia" in it, if clicked takes all the text, would like to avoid this. Thanks in advance for the help!

  • @Matheusazevedo to see if I understand: when the mouse goes over an element (whatever) it adds the class. Then you want to remove the class by clicking on the element and also reading its content is that? and it can be any element or you want to limit it to just a few?

  • @Sergio at click it will pick up the text he clicked, will only remove the class when the user takes the mouse from above, the problem is that when it positions on top of the body, the same stays with the class and this should not occur.

  • @Matheusazevedo then just want to prevent the body get the class is that? rest can give/take class and click on all elements?

  • @Sergio exactly!

  • @There are ways of doing?

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.