views:

54

answers:

4

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

A: 

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.

Paulo Santos
of course there is, whaddya mean?
Anurag
This button has no name, so it's clearly not meant to be submitted as part of a form.
bobince
A: 

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.

Tatu Ulmanen
Here's the button element - http://www.w3.org/TR/html401/interact/forms.html#h-17.5
Anurag
+2  A: 

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];
bobince
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?
salmane
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.
bobince
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 :(
salmane
A: 

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

salmane