Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
how to create textbox using javascript?
Old 06-16-2008, 03:52 AM how to create textbox using javascript?
Novice Talker

Posts: 14
Name: paparanch
Trades: 0
hey guyz!

i have another question for you guyz!

is thier a way to create a textbox using javasccript?

just like this one in html <input type="text"...blah blah...></input>

what is the equivalent code of this in javascript?
paparanch is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-16-2008, 05:21 AM Re: how to create textbox using javascript?
nyef's Avatar
Ultra Talker

Posts: 265
Name: Lucas
Trades: 0
Code:
document.write("<input type=\"text\"...blah blah...></input>");
or
Code:
document.getElementById("somediv").innerHTML="<input type=\"text\"...blah blah...></input>";
__________________
~nyef

Please login or register to view this content. Registration is FREE
nyef is offline
Reply With Quote
View Public Profile Visit nyef's homepage!
 
Old 06-17-2008, 05:16 PM Re: how to create textbox using javascript?
Gilligan's Avatar
Website Designer

Posts: 1,670
Name: Stefan
Location: London, UK
Trades: 0
... there's no such thing as </input>
__________________

Please login or register to view this content. Registration is FREE
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 06-19-2008, 12:44 AM Re: how to create textbox using javascript?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
You will want to use DOM-compliant methodologies. This generalized function should help. The third parameter is an inline object of the form

Code:
{parameter_name:'parameter_value', ...}
Where parameter_name is intentionally not quoted (though, it may be, if desired) and the value is quoted. The colon between them is required. Separated by a comma, you can add as many as you want.

HTML Code:
<html>
  <head>
    <script>
      function addElement(tag_type, target, parameters) {
        //Create element
        var newElement = document.createElement(tag_type);

        //Add parameters
        if (typeof parameters != 'undefined') {
          for (parameter_name in parameters) {
            newElement.setAttribute(parameter_name, parameters[parameter_name]);
          }
        }

        //Append element to target
        document.getElementById(target).appendChild(newElement);
      }
    </script>
  </head>
  <body>
    <div id="targetTag"></div>
    <input type="button" onClick="addElement('INPUT','targetTag',{id:'my_input_tag', name:'my_input_tag', type:'text', size:'5'}); return false;" value="Add Input Tag" />
    <input type="button" onClick="addElement('INPUT','targetTag'); return false;" value="Add Input Tag W/O Parameters" />
  </body>
</html>
EDIT: One note. If you bother to check the DOM after executing this in IE, you'll see that the name field is not available to you. IE declares this field to be read-only which is why you don't see it. The field will be properly submitted, however, as shown in this test code:

PHP Code:
<?php
if (isset($_POST)) {
  print 
'<pre>'.print_r($_POST,true).'</pre>';
}
?>
<html>
  <head>
    <script>
      function addElement(tag_type, target, parameters) {
        //Create element
        var newElement = document.createElement(tag_type);

        //Add parameters
        if (typeof parameters != 'undefined') {
          for (parameter_name in parameters) {
            newElement.setAttribute(parameter_name, parameters[parameter_name]);
          }
        }

        //Append element to target
        document.getElementById(target).appendChild(newElement);
      }
    </script>
  </head>
  <body>
    <form method="post">
    <div id="targetTag"></div>
    <input type="submit" value="Check"/>
    </form>
    <input type="button" onClick="addElement('INPUT','targetTag',{'id':'my_input_tag', 'name':'my_input_tag', 'type':'text', 'size':'5'}); return false;" value="Add Input Tag" />
    <input type="button" onClick="addElement('INPUT','targetTag'); return false;" value="Add Input Tag W/O Parameters" />
  </body>
</html>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE

Last edited by JeremyMiller; 06-19-2008 at 01:05 AM..
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 06-29-2009, 08:31 AM Re: how to create textbox using javascript?
Banned

Posts: 3
Name: Jane Ally
Trades: 0
Your code neatly solved a similar problem I was having.
I'm using it to create text boxes on item selection from drop down list using the following code:
<select name="select1" onchange="addElement('INPUT','targetTag',{id:'my_i nput_tag', name:'my_input_tag', type:'text', size:'5', readonly:'readonly'}); return false;">

My only problem is that I am trying to create the text boxes as readonly as you can see from the code but this is not working.
Any assistance you can offer would be greatly appreciated.
hagsish is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to how to create textbox using javascript?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.41821 seconds with 12 queries