0

Here is my JSFiddle: http://jsfiddle.net/kboucheron/XVq3n/15/

When I start a list of items and I click on "Clear", I would like to text input be cleared as well.I'm not able to clear the fields

<input type="text" placeholder ="Add List" id="listItem"/>
<button id="addButton">add Item</button>
<button id="clearButton">Clear Items</button>
<ul id="output"></ul>

clearButton.addEventListener("click", function(e) {
    var text = document.getElementById('listItem').value;
    var addItem = document.getElementById('output');
    addItem.innerHTML = '';
    text.value = '';
});

3 Answers 3

2

Just need to make this change here:

var text = document.getElementById('listItem');

You had this:

var text = document.getElementById('listItem').value;

What you are doing is getting the value of the input text, when you actually want the input element.

Also here is the updated fiddle: http://jsfiddle.net/XVq3n/16/

Sign up to request clarification or add additional context in comments.

Comments

2

you are referring in your code to input's value, replace

var text = document.getElementById('listItem').value

with

var text = document.getElementById('listItem')

Comments

2

Ok, it's a really simple (but easy to make) error. Try this change and it should work:

clearButton.addEventListener("click", function(e) {
    var text = document.getElementById('listItem');
    var addItem = document.getElementById('output');
    addItem.innerHTML = '';
    text.value = '';
});

Basically, you did .value one too many times. Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.