user299648
user299648

Reputation: 2789

Can I combine two html buttons and their onclick methods using something like javascript?

<html> 
    <div class="button">
         <input type="button" onclick="method1();" id="1" value="Button1"/>
    </div>

     <div class="button">
         <input type="button" onclick="method2();" id="2" value="Button2"/>
     </div>
</html> 

Without having to modify the Html, can I combine the two buttons into one button? And when that button is clicked, can both the methods be called? Can this be done with Javascript/JQuery? If so, how would you do it?

In other words, can I hide one button and fire both methods without changing the html.

Upvotes: 2

Views: 2511

Answers (2)

Seo Gregory
Seo Gregory

Reputation: 17

You can add both method calls to one onclick attribute.

<div class="button">
     <input type="button" onclick="method1();method2();" id="1" value="Button1"/>
</div>

Upvotes: 0

Rolando Cruz
Rolando Cruz

Reputation: 2784

$("#1, #2").click(function(){
   method1();
   method2();
   return false;
});

EDIT

Based on Yardboy's comment the OP might want this instead

$(function(){
  $("#2").parent().hide();
  $("#1").click(function(){
     method1();
     method2();
     return false;
  });
});

Upvotes: 7

Related Questions