0

I found that code on internet and it works fine to create a button

  document.write(nomedispositivo)
  var r=$('<input/>').attr({//início botão
  type: "button",
  id: "field" ,
  value: "Liga",

But if I insert the line: onclick:switchLED() where switchLED is a function the button not appear where is the problem?

  document.write(nomedispositivo)
  var r=$('<input/>').attr({//início botão
  type: "button",
  id: "field" ,
  value: "Liga",
  onclick:switchLED()
1
  • You are executing a function and pass the result value to the onclick, you should pass it as string: onclick:"switchLED()" Commented Mar 15, 2015 at 13:39

1 Answer 1

2

Why add the click handler this way in the first place? You're using jQuery, so use jQuery. Just add the handler to the jQuery element you already have:

var r=$('<input/>').attr({
  type: "button",
  id: "field" ,
  value: "Liga"
});

r.click(switchLED);

Since r is a jQuery element, you can use the click(function) function to add a function reference to that element's click event handler.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.