/    Sign up×
Community /Pin to ProfileBookmark

Javascript array help

Hey guys, I’m very new to this forum so I hope I get a response. I’m taking a javascript class and the teacher is very hard to understand. What I need to do is make an onclick function to add elements to an list through a form. I got that working.

Now I need to have every list item that appears have a delete function also…here’s what I have:

<ul id=”thislist”>
</ul>
<input type=”text” id=”theValue” />
<a href=”javascript:;” onclick=”addElement();”>Add Item</a>

<script language=”javascript” type=”text/javascript”>
<!–
//<![CDATA[
function addElement() {
var value1 = document.getElementById(“theValue”);
var the_text = value1.value;
var anchor_text = document.createTextNode(the_text);
var new_anchor = document.createElement(‘li’);
new_anchor.appendChild(anchor_text);
var list = document.getElementById(‘thislist’);
list.appendChild(new_anchor);
list.innerHTML = (new_anchor, <a href=”javascript:;” onclick=”remove()”>Delete</a>;

}

function remove() {
var test = document.getElementById(‘thislist’);
test.parentNode.removeChild();
}

//]]>
//–>
</script>

It worked without a delete function, until I added the innerHTML part.

Thank You

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@criterion9Feb 09.2009 — 
list.innerHTML = (new_anchor, <a href="javascript:;" onclick="remove()">Delete</a>;
[/QUOTE]

You have a "(" at the beginning and not one at the end. The value that innerHTML expects is a string which is not what you are passing it.

[script]

list.innerHTML = "<a href="javascript:return%20;"

onclick="remove()">Delete</a>";

[/script]
Copy linkTweet thisAlerts:
@bennyblanco604authorFeb 09.2009 — That helps a little...actually first of all, thanks for your response!

That points me in the right direction...What I need to happen is the list shows what is submitted with a delete link next to it and a delete next to every list item.

Is there something really obvious I'm missing? Maybe I've been looking at this code for too long
Copy linkTweet thisAlerts:
@criterion9Feb 10.2009 — You would need to have your <a> tag within the <li> element. When you appendChild your <a> tag should already be there so no need for innerHTML. Also you would need to send a variable to your delete function so it knows which item to delete. Probably the number of the index that is currently being displayed.
×

Success!

Help @bennyblanco604 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.20,
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,
)...