Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I need the caption to display "this is the special button caption" upon clicking "special button" button (which it already does), and I need the caption to display "this is the special button 2 caption" upon clicking "special button 2" button (where I am having problems). Help is much appreciated. Here is the code:

<!DOCTYPE html>

<button onclick="myFunction()" id="jhtp">Special Button</button>
<button onclick="myFunction()" id="hhh">Special Button 2</button>

<p id="demo"></p>

function myFunction()
var x="";
var image1=document.getElementById( "jhtp" ).addEventListener(
  "click", false ); 
var image2=document.getElementById( "hhh" ).addEventListener(
  "click", false ); 

if (onClick="image1")
  x="this is the special button caption";

else if (onClick="image2")
    x="this is the special button 2 caption";
  x="this is the other caption";


share|improve this question

closed as off topic by Brian Reichle, Fge, Glenn Rogers, Jeff Vanzella, Flambino Dec 17 '12 at 1:02

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

This site is a place to get reviews of working code. It's not about getting code to work. –  sepp2k Dec 17 '12 at 12:20

1 Answer 1

Use some jQuery to make working event binding easier.

$(document).ready(function () {
    $("#jhtp").click(function() {
         $("#demo").html("this is the special button caption");
    $("#hhh").click(function() {
         $("#demo").html("this is the special button 2 caption");

Adding the two event listensers inside the myFunction() [which in itself is responding to a click event] isn't necessary.

Here is a good post on why to avoid onclick http://stackoverflow.com/questions/5871640/why-is-using-onclick-in-html-a-bad-practice

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.