0

Using JavaScript, I have created a button with a function that fires when it is clicked. Three parameters are being passed to the function id, clientid, and title. All are strings. Using the first method which can be demonstrated by clicking the Demo1 button, I receive an error of

"Expected ')'"

Using the second method which can be demonstrated by clicking the Demo2 button, I can successfully fire the function, however the log reads as if it is expecting 5 parameters rather than 3.

//console.log(id, clientid, title);
10636,8154,New Opportunity - 1926 Grand Avenue, LLC 5/22/2018 undefined undefined

//console.log(title)
undefined

What can I do to properly pass in title?

(function() {
  var id = "10636";
  var clientid = "8154";
  var title = "New Opportunity - 1926 Grand Avenue, LLC 5/22/2018";
  console.log('<button style="background-color: #00539B; border: 1px solid #00539B; color: white; border-radius: 4px;" onclick="openAddModal(\'' + id + ',' + clientid + ',' + title + '\')" />' + id + "</button>");
  document.getElementById("demo1").innerHTML = "<button style='background-color: #00539B; border: 1px solid #00539B; color: white; border-radius: 4px;' onclick=openAddModal(" + id + ',' + clientid + ',' + title + ")>" + 'Demo1' + "</button>"

  document.getElementById("demo2").innerHTML = '<button style="background-color: #00539B; border: 1px solid #00539B; color: white; border-radius: 4px;" onclick="openAddModal(\'' + id + "," + clientid + "," +  title + '\')" />' + 'Demo2' + "</button>"
})();

function openAddModal(id, clientid, title) {
  console.log(id, clientid, title);
  console.log(title);
}
<span id="demo1"></span>
<span id="demo2"></span>
cfoster5
  • 1,696
  • 5
  • 28
  • 42
  • 5
    don't use the `onclick` attribute of an element; attach a listener after you create the element by using [`addEventListener`](https://developer.mozilla.org/en-US/docs/Web/Events/click) (see the section labeled "Example"). – Dan O Jun 04 '18 at 18:31
  • The second button correctly uses `\'`. But you have to wrap _each argument_ in `\'` `\'`. Any reason you resort to `onclick` attributes rather than event listeners? – Sebastian Simon Jun 04 '18 at 18:32
  • @DanO How can I add an Event Listener if I create my button using a `return`? Like: `return ""` – cfoster5 Jun 04 '18 at 19:15

0 Answers0