/    Sign up×
Community /Pin to ProfileBookmark

Removing dynamically created elements

I am creating textboxes dynamically using the following code:

[INDENT][COLOR=”Blue”] function AddElement(type,i){ //Add the element specified in the parameter
var value=”Value”;
var element = document.createElement(“input”); //Create an input type dynamically
var newline = document.createElement(“br”); //Create an input type dynamically

element.setAttribute(“type”, type); //Assign different attributes to the element
element.setAttribute(“name”, “vals[]”);
element.setAttribute(“id”, value+i);
element.setAttribute(“value”, value+i);
element.setAttribute(“onblur”,”return IntegerOnly(‘Value”+i+”‘, ‘observation.’)”);

//Append the element in page
var ManInputBoxes = document.getElementById(“spanManualInput”);
ManInputBoxes.appendChild(newline);
ManInputBoxes.appendChild(element);
}[/COLOR][/INDENT]

i create a newline between each text box

I want to have a function that will remove textboxes and the newline that is created. I have the following function to remove the text boxes but I cant remove the new lines that were created so an empty space is left on the page.
[INDENT][COLOR=”blue”] function RemoveElement() {
var d = document.getElementById(‘spanManualInput’);
var oldbox = document.getElementById(‘Value’+i);
d.removeChild(oldbox);
d.removeChild(‘br’);
}[/COLOR]
[/INDENT]

any help is appreciated!

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@TcobbMar 24.2011 — I haven't tried out your code. I will assume it does work, except for getting rid of the <BR> elements. In your function RemoveElement I might suggest substituting this for the last two lines of your code:

[CODE]d.removeChild(oldbox.nextSibling);
d.removeChild(oldbox);[/CODE]
Copy linkTweet thisAlerts:
@claireneedshelpauthorMar 24.2011 — thanks Tcobb but unfortunately that doesnt do anything.

Yeh you are right in thinking....my code is working for removing the textbox but not the br
Copy linkTweet thisAlerts:
@cbVisionMar 24.2011 — What's the DOM look like? Can you paste the HTML it's actually displaying?
Copy linkTweet thisAlerts:
@claireneedshelpauthorMar 24.2011 — [INDENT][COLOR="Blue"] <div id="divManualUpload" >

<b>Enter the number of observations: </b>

<input name="txtNoOfValues" type="text" id="NoOfValues" onblur="return IntegerOnly('NoOfValues', 'number of values.')"/>

<span id="spanManualInput"><input onclick="AddValues('Textbox')" type="button" value="Add" name="cmdAddValues"/></span> <br />

</div>[/COLOR]
[/INDENT]

thats where the code is called

AddValues is basically just a for loop that loops through from 0 to the number of values entered and calls AddElement for each value entered
Copy linkTweet thisAlerts:
@TcobbMar 25.2011 — Whoops--for some reason I thought you were appending a <BR> element after the checkbox--not before. In that case instead of "d.removeChild(oldbox.nextSibling);" it should be "d.removeChild(oldbox.previousSibling);"
Copy linkTweet thisAlerts:
@claireneedshelpauthorMar 25.2011 — amazing! thank you it works perfectly! ?
Copy linkTweet thisAlerts:
@TcobbMar 25.2011 — You're welcome. Sorry about my original post. It is often the case with me that when it comes to paying attention I tend to do it with a hot check.
×

Success!

Help @claireneedshelp 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...