Function to show the id of the elements of a page in a span id="span" / span

0

The javascript should be with event onmouseover

function mostrarID() {
.........................
.........................
}
document.onmouseover = mostrarID;

HTML example

O ID é:"<span id="span" style="font-weight:bold"></span>"<br>
<p id="p1">Paragrafo</p>
<div id="Div1">
<p id="p2">Paragrafo2</p>
<hr id="hr1">
<a href="#" id="Home">Home</a>
</div>
<p id="p3">Paragrafo3</p>
<a href="#" id="nav-questions">Perguntas</a>
<p id="p4">Paragrafo4</p>
<a href="#" id="Tags">Tags</a>

<div id="Div2">
<form id="Form">
<input id="Input" type="text"/>
</form>
</div>
    
asked by anonymous 10.05.2017 / 23:47

1 answer

1

Just find the id attribute of the element in question with event.target.getAttribute and set the value in the desired span .

const span = document.getElementById("span");

document.onmouseover = function (event) {
  span.innerHTML = event.target.getAttribute("id");
}
O ID é:"<span id="span" style="font-weight:bold"></span>"<br>
<p id="p1">Paragrafo</p>
<div id="Div1">
  <p id="p2">Paragrafo2</p>
  <hr id="hr1">
  <a href="#" id="Home">Home</a>
</div>
<p id="p3">Paragrafo3</p>
<a href="#" id="nav-questions">Perguntas</a>
<p id="p4">Paragrafo4</p>
<a href="#" id="Tags">Tags</a>

<div id="Div2">
  <form id="Form">
    <input id="Input" type="text" />
  </form>
</div>
    
11.05.2017 / 00:03