Converting a textbox into any other html element
<form name="txt" method="post">
<input type="text" name="select" id="select" onclick="return selectbox();">
</form>
Now through js or html can I change my textbox into a select or a list box. Is it possible by any way.
//js code
function selectbox()
{
var s开发者_开发技巧elect=document.getElementById('select');
select.innerHTML="<select><option>1</option><option>2</option></select>";
}
Something like this should happen. In the place of the textbox the below code should appear //new textbox
<select><option>1</option><option>2</option></select>
Yes, for example:
var select = document.createElement("select");
select.innerHTML = "<option value='1'>One</option>" +
"<option value='2'>Two</option>" +
"<option value='3'>Three</option>";
select.id = "select";
select.name = "select";
select.onclick = function(){return selectbox()};
var currentSelect = document.getElementById("select");
currentSelect.parentNode.replaceChild(select, currentSelect);
select.focus(); //Focus on the element
This code snippet creates a <select>
element, and adds new options to it through .innerHTML
. Then, the attributes are set. Finally. the script selects the current element with id="select"
from the HTML document, and replaces the element by the newly created element.
Be more specific about what and how. You can try following
function selectbox(Sender){
var select = document.createElement('select');
//UPDATE
for(var i=1; i<=2; i++)
select.options[select.options.length] = new Option(i, i);
Sender.parentNode.appendChild(select);
Sender.parentNode.removeChild(Sender);
}
精彩评论