Jump to content

Archived

This topic is now archived and is closed to further replies.

kharbat

A question related to CreateElement.. i think

Recommended Posts

i have a form and button..

when button is clicked a text filed appears in the form..

how can i get this done??

Share this post


Link to post
Share on other sites
It depends how you set it up, but the easiest will be something like this
[code]
<table>
<tr id="hidden_field_tr" style="display:none;">
<td><input type="text" /></td>
</tr></table>

<button onclick="document.getElementById('hidden_field_tr').style.display='';">Show</button>

<button onclick="document.getElementById('hidden_field_tr').style.display='none';">Hide</button>
[/code]

Share this post


Link to post
Share on other sites
thanks for that... but in fact i want to create the text field.. i mean that i don't want it to be pre defined because i don't know how many text fields gonna be created in the page.. you got my point?

i need a script that creates a text field from nothing.. and puts it within a specific form and that's all..

Share this post


Link to post
Share on other sites
you can do something like this
[code]
<script language="javascript">
    function create_element(){
        var form_tbody = document.getElementById('form_tbody');
        var n_tr = document.createElement("TR");
        var n_td = document.createElement("TD");
        var n_input = document.createElement("INPUT");
        
        n_input.name = "input_name";
        n_input.id = "input_id";
        n_input.type = "text";
        
        form_tbody.appendChild(n_tr);
        n_tr.appendChild(n_td);
        n_td.appendChild(n_input);

    }
    
</script>

<table>
    <tbody id="form_tbody">
        <tr><td><input type="text" /></td></tr>
    </tbody>
</table>
[/code]

Just remmeber that you need to tbody if you are using tables.

Share this post


Link to post
Share on other sites
i am not using tables actually.. but your script was pretty helpful.. i will try it and tell you what happens

Share this post


Link to post
Share on other sites
now what's wrong with this script ? [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]


[code]

<script language="javascript">
file = document.createElement("INPUT");
file.name = "t1";
file.type = "text";

forms = document.getElementById('form1');

form.appendChild(file);

</script>

<form id="form1" name="form1" method="post" action="">
  <label>
  <input type="button" name="Button" value="Button" />
  </label>
</form>

[/code]

Share this post


Link to post
Share on other sites
You are creating the input field before the page loads, the javascript cannot find the forms because it haven't loaded yet.

You'll need to set your script as a function and call it when the page loads.

(btw you are missing an s in your form.appendChild(file)- should be forms)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.