Bart
Bart

Reputation: 172

Cannot clear input field

I'm creating a filtered table in JavaScript. Everything is okay. However, the only line that doesn't seem to work is inputValue = ''. Not sure why it doesn't want to clear the field after filtering is done.

If you replace that with document.querySelector('.form__input').value things seem to work, but I don't want to repeat the same code. I already declared it above as inputValue.

const initValues = [
	'Walmart',
	'State Grid',
	'Sinopec Group',
	'China National Petrolium',
	'Royal Dutch Shell',
	'Toyota Motor',
	'Volkswagen',
	'BP',
	'Exxon Mobil',
	'Berkshire Hathaway'
];

const tableCreation = array => {
	const tableBody = document.querySelector('.table__body');
	document.querySelectorAll('tr').forEach(el => el.parentNode.removeChild(el));
	array.forEach(el => {
		const row = document.createElement('tr');
		const cell = document.createElement('td');
		const cellText = document.createTextNode(el);
		cell.appendChild(cellText);
		row.appendChild(cell);
		tableBody.appendChild(row);
	});
};

tableCreation(initValues);

const filterTable = event => {
	event.preventDefault();
	let inputValue = document.querySelector('.form__input').value;
	const filtered = initValues.filter(el => el.toLowerCase().includes(inputValue.toLowerCase()));
	if (filtered) {
		inputValue ? tableCreation(filtered) : tableCreation(initValues);
	}
	inputValue = '';
};

document.querySelector('.form__button').addEventListener('click', filterTable);
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<link rel="stylesheet" href="./css/3.css">
	<title>Filtered list</title>
</head>
<body>
	<form class="form" id="form">
		<label for="filter">Filtered: </label>
		<input class="form__input" type="text" id="filter" name="input" placeholder="Insert phrase...">
		<button class="form__button" form="form" type="submit">Filter</button>
	</form>

	<table class="table">
		<tbody class="table__body"></tbody>
	</table>

	<script src="./js/3.js"></script>
</body>
</html>

Upvotes: 0

Views: 904

Answers (3)

ttulka
ttulka

Reputation: 10882

The variable inputValue is holding only the actual value of the field, it's detached from it.

You can save a reference to the field as a variable and clean the value as follows:

const inp = document.querySelector('.form__input');
inp.value = '';

Upvotes: 0

sarvon ks
sarvon ks

Reputation: 616

You already get value only from inputvalue., but u can't change that value so get dom instance also kindly change this code to

const filterTable = event => {
    event.preventDefault();
    let inputElement = document.querySelector('.form__input'),
        inputValue = inputElement.value;
    const filtered = initValues.filter(el => el.toLowerCase().includes(inputValue.toLowerCase()));
    if (filtered) {
        inputValue ? tableCreation(filtered) : tableCreation(initValues);
    }
    inputElement.value = '';
};

Upvotes: 1

RenaudC5
RenaudC5

Reputation: 3829

let inputValue = document.querySelector('.form__input').value;

this line return the string value of the input. When you are trying inputValue = ''; you are only changing the value of the variable 'inputValue' but not of the input field.

to do this juste save you field as a variable instead of it's value and then change it's value :

let inputField = document.querySelector('.form__input');
const filtered = initValues.filter(el => el.toLowerCase().includes(inputValue.toLowerCase()));
if (filtered) {
    inputValue ? tableCreation(filtered) : tableCreation(initValues);
}
inputField.value = '';

Upvotes: 1

Related Questions