views:

270

answers:

2

Hi Everyone:

I am working on a fairly dynamic site, and I want the user to be able to choose what they want to input. The input choices are the various types of input (text, password, etc.) Is there some style/way to create this? I was thinking that they choose from a drop down menu and then Javascript takes care of the rest. But I would think that there must be some established way to deal with this that I'm just not finding on the web.

Thanks for any help.

+1  A: 

In Javascript, you would create an input as follows.

var input = document.createElement('input');
input.type = 'button';
...

So, if you would like to create inputs on the fly, you could create a dropdown that lists the types of inputs as strings (button, text, etc.). Then, once the user had chosen the string in the dropdown, you would pass it to a Javascript function like the following:

function createInput(type) {
  var input = document.createElement('input');
  input.type = type
  ...
  return input;
}

Then, if you wanted to append the input to an element on the page with id 'foo':

var input = createInput('button');
var appendToThis = document.getElementById('foo');
appendToThis.appendChild(input);

If you would like to start with a div on the page, imagine you have a div tag with id foo on the page:

<div id=foo><input type=text></div>

Then, when the user chooses an item, clear the div and make a new input:

function whenUserChoosesAType(type) {
  var div = document.getElementById('foo');
  rac(div);
  var input = document.createElement('input');
  div.appendChild(input);
}      

//abbreviated function to clear the contents of a DOM element such as a div
function rac(elem) {
  while(elem.hasChildNodes()) elem.removeChild(elem.firstChild);
}
Andrew Johnson
While I'll have to try this out and see if it works with my setup, it looks great and I thank you for your help!
PF1
I just tried implementing the code, but am running into a bit of a problem. That problem being that I want a default input field to be there, but then the drop down menu will give the user the ability to change it. Is there some way to replace the other input field when I run this script?
PF1
Sure, just create a div using Javascript, like I do the input above. Then create a default input and append it to the div. When the user selects a new input type, empty the div, and append a new inpout of the appropriate type to it.I like to abbreiavte the function to clear a div (or other element) as follows: function rac(obj) { while(obj.hasChildNodes()) obj.removeChild(obj.firstChild); }
Andrew Johnson
A: 

I originally answered your questions like this:

<input type="text" id="changingInput">
<input type="button" value="Click Here For Example Of Changing" onclick="javascript:document.getElementById('changingInput').type = 'password';">

But then I checked and it didn't work in IE... or course.(it DOES work wonderfully in Safari) So i found this great link here:

http://www.universalwebservices.net/web-programming-resources/javascript/change-input-element-type-using-javascript

Hope this helps!

micmoo
Thanks for the link, I'll check it out!
PF1