How to run a javascript referencing a button ID?

Asked

Viewed 246 times

1

I need to run a javascript, plus I would like to do this using the ID of the button that clicked,

Example:

$(Document). ready(Function(){

  $("p").click(function(){
      alert("estou usando o P.");
  });

  $('teste').on('click',function(){
        //eu precisa de alguma coisa assim
         alert("estou usando o id."); 
  });

});

How to use id reference?.

In this example only works when using the "P tag"

Thanks

  • reformulates the question that was confused.

  • I’ll do it now

1 answer

0


var btn1 = document.querySelector("#mostrarcampo");
btn1.addEventListener('click',function(e){
    document.querySelector("#extratres").style.display = 'block';
 });

This does the same as the

$('#mostracampo1').on('click',function(){
    //eu precisava de alguma coisa assim
    document.getElementById("extratres").style.display = 'block';  

});

  • the excerpt you show does not work, I already tested

  • I changed my question to facilitate understanding

  • You’re right, I took the test and it worked

Browser other questions tagged

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