/    Sign up×
Community /Pin to ProfileBookmark

duplicate a field

For a page contains INPUT

[code]
<body>

<div id=”p”>
<input type=”text” class=”abc” id=”inp1″ name=”inp1″ value=”” maxlength=30 />
</div>

</body>
[/code]

Now I want to dynamically duplicate the input field, i.e., copy all the information except the id and name, what should be the javascripts for this?

After duplicate:

[code]
<div id=”p”>
<input type=”text” class=”abc” id=”inp1″ name=”inp1″ value=”” maxlength=30 />

<input type=”text” class=”abc” id=”inp2″ name=”inp2″ value=”” maxlength=30 />

<input type=”text” class=”abc” id=”inp3″ name=”inp3″ value=”” maxlength=30 />

</div>
[/code]

Thanks

Scott

I tried something like this:

[code]
x= document.createElement(“input”);
x.id=”inp2″;
x.name= “inp2”;
x.maxlength=document.getElementById(‘inp1’).maxlength;
x.classname=document.getElementById(‘inp1’).classname;
x.style.top=document.getElementById(‘inp1’).offsetTop + document.getElementById(‘inp1’).offsetHeight + 5;
x.style.left=document.getElementById(‘inp1’).offsetLeft;

document.getElementById(‘p’).appendChild(x);
[/code]

I mean, is there a better way to do this?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 08.2008 — use cloneNode() method:
<i>
</i>var clone=document.getElementById('inp1').cloneNode(true);
clone.id='inp2';
clone.name='inp2';
document.getElementById('p').appendChild(clone);
Copy linkTweet thisAlerts:
@TeufelMay 08.2008 — Quick and Dirty:

document.getElementById("p").innerHTML += document.getElementById("p").innerHTML
Copy linkTweet thisAlerts:
@KorMay 08.2008 — Quick and Dirty:

document.getElementById("p").innerHTML += document.getElementById("p").innerHTML[/QUOTE]

[B]innerHTML[/B] is not a standard DOM method. Id does not really insert elements into the DOM tree, thus later you will not be able to handle them as part of the DOM.
×

Success!

Help @scottjsn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...