0

in creating a button from an javascript object i am doing the following :

    for (buttonName in buttons){ 
         var htmlbutton = '<button type="button" onclick="'+buttons[buttonName]()+'">'+buttonName+'</button>' 
}

I am trying to attach the function to an onclick event but i cant figure out the right sythanx

thanks in advance

3
  • what exactly is that onclick function supposed to do? Commented Jan 5, 2010 at 23:57
  • type="button" is redundant and probably invalid Commented Jan 6, 2010 at 0:00
  • It's valid, and it's required for IE, which incorrectly defaults button-type to submit. Commented Jan 6, 2010 at 0:05

4 Answers 4

2

You can't embed a Function instance inside a string.

If the buttons variable is global, it will be accessible to code inside the onclick event handler attribute:

var htmlbutton= '<button type="button" onclick="buttons[\''+buttonName+'\']()">'+buttonName+'</button>' 

However, there is no escaping in the above, so you will have problems (potentially security problems) if buttonName can ever contain a <, &, ' or " character. You would have to escape these in various ways for the JavaScript and HTML you're embedding the strings in.

Much better is to avoid slinging strings to make HTML at all. Then you can use DOM methods and native JavaScript objects and avoid all the confusing nastiness of strings-inside-strings.

var button= document.createElement('button');
button.type= 'button';
button.appendChild(document.createTextNode(buttonName));
button.onclick= buttons[buttonsName];
Sign up to request clarification or add additional context in comments.

3 Comments

I appreciate this alternate methods and I clearly see the security advantage. However i am stuck in implementing it in my code in which i create different properties to an object before i create a dom element from this object. any suggestions?
You could make a temporary lookup of properties for assigning to the DOM node later, I suppose. I'm not sure exactly why you can't create the nodes as you go along, though... you don't have to actually insert them into the document until later if you don't want to.
the context is a modalbox ( i know there many scripts out there for that but I am using this as an excuse to dive deeper in javascript) what i am doing creating a modalbox piece by piece.to be more accurate I am creating a modalbox object piece by piece and at the end i want to append on of the object properties ( the finalized box). Perhaps I am going about this the wrong way :(
0

Try to replace your code with:

for (buttonName in buttons)
{
     var htmlbutton = 
         '<input' + 
         ' type="button"' +
         ' value="' + (buttons[buttonName]).replace(/"/gi, "&quot;") + '"' +
         ' onclick="' + (buttons[buttonName]).replace(/"/gi, "&quot;") + '()"/>';
}

Edited to clarify:

As there's no reliable <button> tag in HTML.

From W3School website:

Important: If you use the button element in an HTML form, different browsers will submit different values. Internet Explorer will submit the text between the <button> and </button> tags, while other browsers will submit the content of the value attribute. Use the <input> element to create buttons in an HTML form.

1 Comment

This button has no name, so it's clearly not meant to be submitted as part of a form.
0

I guess you're looking for something like this:

for(buttonName in buttons) {
    var htmlbutton = '<button type="button" onclick="window.buttons[\''+buttonName+'\']()">'+buttonName+'</button>';

Instead of printing the whole function in the onclick attribute, this just prints out the callable function name. Just make sure that buttons is in the correct scope.

1 Comment

0

Though using jquery isnt required in this case, the code i am writing is for a jquery plugin. So far this is the best solution i could come up with :

$.each(buttons, function(name, fn) {   

   $('<input type="button" value="'+name+'" />').click(function() { fn.apply() }); 


  });

I have yet to find out whether there are any pitfalls in using the call or apply property but so far this works like charm.

your thoughts and comments are very well appreciated

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.