Forum Moderators: open

Message Too Old, No Replies

Making new button on fly with javascript

         

Lookie

9:51 am on Apr 21, 2005 (gmt 0)

10+ Year Member



Hello again!

I need to generate a button on the fly that will open print window when user hits preview button on the previous page. Here is what i have so far:

elem = document.createElement("input");
elem.setAttribute("id", "btnPrint");
elem.setAttribute("value", "print document");
elem.setAttribute("alt", "print document");
elem.setAttribute("type", "button");
elem.setAttribute("onClick", "window.print()");
document.getElementsByTagName("body")[0].appendChild(elem);

What I would like to solve are two things: that button would *actually* appear on the newly created page and that it would be on top of the document (i guees i just call the function before any other body elements render). Any suggestions on how to make this button? Thanks in advance for reply.

orion_rus

10:13 am on Apr 21, 2005 (gmt 0)

10+ Year Member



To create element in a new window
you should make this:
newwindow=window.open([someurl,options if needed]);
then make this:
elem = document.createElement("input");
newwindow.document.body.appendChild(elem);
also i suggest you not use setAttribute because it had bad crossbrowsing use simple like:
elem.id='elem'; and so on

to make button on top of the text you should use css styles like zIndex or make style - position:absoulte
on the top of this page in the current location
good luck to you

Lookie

9:21 am on Apr 28, 2005 (gmt 0)

10+ Year Member



Hi!

Thanks for reply. Button is created on fly with no problem now the thing is how to set onclick event to the newly created element

elem = document.createElement("input");
elem.id ='btnGumb';
elem.value = 'x close';
elem.type = 'button';
elem.onclick = window.close();
document.body.appendChild(elem);

i donno how to set onclick event in another meaningful way so that created button will respond to it. any thoughts? thanks.

Bernard Marx

3:23 pm on Apr 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



elem.onclick = function(){window.close() };