views:

21

answers:

2

Is it possible to have a javascript bookmarklet to add in a new input box into an existing page? Basically I need to have a prompt box to save a variable but I don't want it to pop up, I want it just in the page so I can then have it filled out and submit so the variable can be used in another piece of javascript. Its kind of confusing, lol, but just wondering if this is possible.

Thanks

A: 

Yes, it's possible.

For example:

javascript:void(document.body.appendChild(document.createElement('input')));

would simply append an input-box to the document's body.

The void() is needed because of appendChild() has a return-value, so if you dont use void, your location will be forwarded to that return-value.

Dr.Molle
Perfect. thank you. How would I go about giving that input box some properties, specifically a value that I can then read with another javascript later.
Seatbelt99
A: 

You can add attributes like you usually do in Javascript. Save the created element into a variable, and then assign the attributes.

An example, how to do this:

javascript:void((function(){var obj=document.body.appendChild(document.createElement('input'));obj.value='someValue';alert('It works, value is \n'+document.body.lastChild.value);})());
Dr.Molle