Jump to content


Photo

A question related to CreateElement.. i think


  • Please log in to reply
6 replies to this topic

#1 kharbat

kharbat
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 13 June 2006 - 05:31 PM

i have a form and button..

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

how can i get this done??

#2 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 13 June 2006 - 06:04 PM

It depends how you set it up, but the easiest will be something like this
<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>


NoGray.com


#3 kharbat

kharbat
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 13 June 2006 - 09:37 PM

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..


#4 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 13 June 2006 - 10:00 PM

you can do something like this
<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>

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

NoGray.com


#5 kharbat

kharbat
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 13 June 2006 - 10:06 PM

i am not using tables actually.. but your script was pretty helpful.. i will try it and tell you what happens

#6 kharbat

kharbat
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 13 June 2006 - 10:17 PM

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\" /]



<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>



#7 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 14 June 2006 - 05:15 PM

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)

NoGray.com





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users