Bruno Moraes
Bruno Moraes

Reputation: 13

Can't clear inputs in Javascript

I'm doing practical Javascript course on watchandcode.com and my code is exactly the same as the instructor's, but the part where it clears the inputs does not work here.

changeTodo: function () {
    var changeTodoPositionInput = document.getElementById('changeTodoPositionInput').valueAsNumber;
    var changeTodoTextInput = document.getElementById('changeTodoTextInput').value;

    todoList.changeTodo(changeTodoPositionInput, changeTodoTextInput);

    // THIS PART DOESN'T WORK
    changeTodoPositionInput.value = '';
    changeTodoTextInput.value = '';
}

The function does its job, it's just those last two lines that don't work.

Upvotes: 1

Views: 73

Answers (2)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

You're trying to clear a value and not input elements, you have to get the DOM elements first :

var changeTodoPositionInput = document.getElementById('changeTodoPositionInput');
var changeTodoTextInput = document.getElementById('changeTodoTextInput');

Then get the value after that from them :

todoList.changeTodo(changeTodoPositionInput.valueAsNumber, changeTodoTextInput.value);

And now you could clear the value of the both inputs using :

changeTodoPositionInput.value = '';
changeTodoTextInput.value = ''; 

FULL CODE :

changeTodo: function() {
    var changeTodoPositionInput = document.getElementById('changeTodoPositionInput');
    var changeTodoTextInput = document.getElementById('changeTodoTextInput');

    todoList.changeTodo(changeTodoPositionInput.valueAsNumber, changeTodoTextInput.value);

    changeTodoPositionInput.value = '';
    changeTodoTextInput.value = ''; 
}

Hope this helps.

Upvotes: 2

Elvin
Elvin

Reputation: 1

You can try this:

var changeTodoPositionInput = document.getElementById('changeTodoPositionInput').value = "";

var changeTodoTextInput = document.getElementById('changeTodoTextInput').value = "";

Upvotes: 0

Related Questions