Pass an HTML element by parameter on onclick

0

I have an HTML, where when doing the event mouseover on a button, an input: text, and a p, these are hidden, and then when clicking on another p these are shown again, the problem is that the tag p I have it like this

<p id="show" onclick="show()">Pulsa aquí para que todo aparezca</p>

and I want it to be that way

<p id="show" onclick="show(button, input, p)">Pulsa aquí para que todo aparezca</p>

for later in JS do

function show(button, input, p) { button.style.display = "initial" input.style.display = "initial" p.style.display = "initial" }

the code is:

<center>
    <div id="app">
        <button id="boton" onmouseover="hide(this)">Botón que desaparece</button>
        <br>
        <br>
        <input type="text" name="txt" id="txt" value="Caja que desaparece" onmouseover="hide(this)">
        <br>
        <p id="text" onmouseover="hide(this)">Texto que desaparece</p>

        <p id="show" onclick="show()">Pulsa aquí para que todo aparezca</p>
        <!-- en show(), quiero pasar por parametro el button, el input y el p -->
        <!-- que quede show(button, input, text) -->
        <!-- para poder ocultarlos al hacer click -->
    </div>
</center>

<script>
    let boton = document.getElementById("boton")
    let txtBox = document.getElementById("txt")
    let text = document.getElementById("text")

    function hide(e) {
        e.style.display = "none"
    }

    function show() {
        boton.style.display = "initial"
        txtBox.style.display = "initial"
        text.style.display = "initial"
    }
</script>
    
asked by darioxlz 20.09.2018 в 02:08
source

5 answers

2

I think you're getting dizzy in a punctual problem. The statement of an inline handler as "onclick" has limitations. In particular, it acquires an implicit bind with the element that declares it. Any other parameter you have to put it hard or calculate it elsewhere.

Let's say you have the function as you want it:

function show(boton, txt, text) {
     boton.style.display = "initial"
     txtBox.style.display = "initial"
     text.style.display = "initial"
}

Since it does not make sense to pass the elements in hard to the inline function, you can resort to an auxiliary function toggle such that:

function toggle() {
    let boton = document.getElementById("boton"),
        txtBox = document.getElementById("txt"),
        text = document.getElementById("text");
    show(boton, txtBox, text);
}

and we leave your element as:

<p id="show" onclick="toggle()">Pulsa aquí para que todo aparezca</p>

In your question you pose:

  

in show (), I want to go through the button, the input and the p parameter   show (button, input, text) to hide them when you click

It gives me the idea, then, that what you want to do in p#show is a toggle. The first click turns everything on, the second click turns everything off. For this you could handle a "state" by means of an attribute of the element. Something simple like the "title":

<p id="show" onclick="toggle(this)" title="encender">Pulsa aquí para que todo aparezca</p>

If the function toggle verifies that the title is "turn on" it should turn on everything and change the title to "turn off". The next click detects that you must "turn off" and turn off everything before returning to the original title.

function toggle(element) {
  let boton = document.getElementById("boton"),
    txtBox = document.getElementById("txt"),
    text = document.getElementById("text");
  if (element.title === 'encender') {
    show(boton, txtBox, text);
    element.title = 'apagar';
    element.innerText = 'Pulsa para apagar todo';
  } else {
    hide(boton);
    hide(txtBox);
    hide(text);
    element.title = 'encender';
    element.innerText = 'Pulsa para que todo aparezca';
  }
}

function hide(e) {
  e.style.display = "none"
}

function show(boton, txt, text) {
  boton.style.display = "initial"
  txt.style.display = "initial"
  text.style.display = "initial"
}
<div id="app">
  <button id="boton" onmouseover="hide(this)">Botón que desaparece</button>
  <br>
  <br>
  <input type="text" name="txt" id="txt" value="Caja que desaparece" onmouseover="hide(this)">
  <br>
  <p id="text" onmouseover="hide(this)">Texto que desaparece</p>

  <p id="show" onclick="toggle(this)" title="encender">Pulsa aquí para que todo aparezca</p>

</div>
    
answered by 20.09.2018 / 03:30
source
2

You can pass the id's of the elements, example:

<p id="show" onclick="show('boton','txt')">Pulsa aquí para que todo aparezca</p>

function show(button,text) {
        document.getElementById(button).style.display = "initial"
        document.getElementById(text).style.display = "initial"
    }

Another way to do it would be to pass the current element and navigate to the parent node, this is useful when you have several sections with the same operation:

<p id="show" onclick="show(this)">Pulsa aquí para que todo aparezca</p>

function show(e) {
        parent_node = e.parentNode;
        e.style.display = "initial"
        parent_node.getElementById('boton').style.display = "initial"
        parent_node.getElementById('txt').style.display = "initial"
    }
    
answered by 20.09.2018 в 02:32
2

You can do it in the following way I get it by the id of the tag.

<center>
<div id="app">
    <button id="boton" onmouseover="hide(this)">Botón que desaparece</button>
    <br>
    <br>
    <input type="text" name="txt" id="txt" value="Caja que desaparece" onmouseover="hide(this)">
    <br>
    <p id="text" onmouseover="hide(this)">Texto que desaparece</p>

    <p id="show" onclick="show(document.getElementById('boton'),document.getElementById('txt'),document.getElementById('text'))">Pulsa aquí para que todo aparezca</p>
    <!-- en show(), quiero pasar por parametro el button, el input y el p -->
    <!-- que quede show(button, input, text) -->
    <!-- para poder ocultarlos al hacer click -->
</div>

<script>
function hide(e) {
    e.style.display = "none"
}

   function show(button,input,p) {
      button.style.display = "initial"
      input.style.display = "initial"
      p.style.display = "initial"
}

    
answered by 20.09.2018 в 02:39
1

Well this is similar to the previous answer, but you can pass the ids you want and show them

 
    let boton = document.getElementById("boton")
    let txtBox = document.getElementById("txt")
    let text = document.getElementById("text")

    function hide(e) {
        e.style.display = "none"
    }



    function show() {

 Array.forEach( arguments, (item)=> {  
 let ele = document.getElementById( item);
 ele.style.display = "initial";

} ); 


    }
<!DOCTYPE html>
<html>

<head>
  
</head>

<body>

<center>
    <div id="app">
        <button id="boton" onmouseover="hide(this)">Botón que desaparece</button>
        <br>
        <br>
        <input type="text" name="txt" id="txt" value="Caja que desaparece" onmouseover="hide(this)">
        <br>
        <p id="text" onmouseover="hide(this)">Texto que desaparece</p>

        <p id="show" onclick="show( 'boton', 'txt', 'text')">Pulsa aquí para que todo aparezca</p>
        <!-- en show(), quiero pasar por parametro el button, el input y el p -->
        <!-- que quede show(button, input, text) -->
        <!-- para poder ocultarlos al hacer click -->
    </div>
</center>



</body>
    
answered by 20.09.2018 в 02:34
1

What you try in vanilla, I do not know if it is possible. What you can do is pass by parameter the class name of the desired elements or, failing that, the ID of those elements. Just remember that if you are going to work with ID's, each of them must be unique. There can not be two elements with the same ID.

Here is an example with vanilla:

function doAction(ele, param1, param2) {
  var a = document.getElementById(param1).innerHTML;
  var b = document.getElementById(param2).innerHTML;
  ele.innerHTML = a + " " + b;
}
<p onclick="doAction(this, 'hello', 'world')">Click en mi</p>

<p id="hello">Hola</p>
<p id="world">Mundo!</p>

Greetings!

    
answered by 20.09.2018 в 02:38