0

I'm making a card game in Javascript and one of the things the webpage does is create buttons with the name of a card that you click to use the card. At least, in theory.

Code:

function addButton(card) {
    var newbutton = document.createElement("input");

    newbutton.setAttribute("type", button);
    newbutton.setAttribute("id", 'someButton');
    newbutton.setAttribute("value",card.displayName);
    newbutton.setAttribute("name",card.name);
    newbutton.setAttribute("onclick",'useCard(name)'); ///this is what's not working

What I'd like is for the onclick attribute to use the named card (the card's name is card.name, which might be different from its displayed name). Instead, it's using useCard with 'name' as the argument, which is obviously wrong. What's the correct line of code here?

2
  • Use a template. What you have is a lot of unneeded crap-code. Commented Oct 3, 2013 at 19:59
  • button and name are variables from an outer scope? Commented Oct 3, 2013 at 20:02

4 Answers 4

3

Adding an event listener would be a better and cleaner option:

newbutton.addEventListener('click', function () {
  useCard(this.getAttribute("name"));
});

If you really need to support IE < 9, you have to check for addEventListener compatibility first and use attachEvent in case the former isn't supported:

// generic helper function
function addEventListenerX(elem, evt, listener) {
  if ("addEventListener" in window) {
    elem.addEventListener(evt, listener);
  }
  else {
    elem.attachEvent("on" + evt, listener);
  }
}


addEventListenerX(newbutton, 'click', function () {
  useCard(this.getAttribute("name"));
});

If you're already using a JS framework like jQuery or prototype, use their listener system. They will automatically add compatibliity for you.

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

3 Comments

@AlienWebguy I've added a patch ;)
Better: On page load: var on = 'addEventListener' in window ? 'addEventListener' : 'attachEvent';. Then, when binding event handlers: newbutton[on]('click', useCardListener);
@ŠimeVidas That won't work because attachEvent requires events to be prefixed with 'on'. I've added a helper function, now.
1

Replace this:

newbutton.setAttribute("onclick",'useCard(name)'); ///this is what's not working

with this:

if (newbutton.addEventListener) {
    newbutton.addEventListener('click', function() { useCard( card.name ); });
else {
    newbutton.attachEvent('onclick', function() { useCard( card.name ); });
}

Comments

0

Try this:

 newbutton.setAttribute("onclick",'useCard("' + card.name + '")');

Comments

0

Use this

 newbutton.setAttribute("onclick",'useCard('+card.name+')'); 

in place of

 newbutton.setAttribute("onclick",'useCard(name)'); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.