Add and Remove Products HTML Activity
<!DOCTYPE html>
<html>
<head>
Add and Remove
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1 class="font1"> Products and Price Select box</h1>
<div id="container">
<form>
<fieldset>
<label for="name"> * add products: </label>
<input type="text" id="name" placeholder="Enter Product" autocomplete="off"> <br> <br>
<label for="price"> * add price: </label>
<input type="number" id="price" placeholder="Enter Price" autocomplete="off"> <br> <br>
<br>
<button class="btn" id="btnAdd" size=10> <b> Add Product </b> </button> <br>
<div class="row">
<div class="column">
<label for="list"> * Product List: </label> <br>
<select id="list" name="list" multiple size=10>
<option value="apple"> Apple </option>
<option value="Banana"> Banana </option>
</select>
</div>
<div class="column">
<label for="list"> * Price List: </label> <br>
<select id="price-list" name="price-list" multiple size=10>
<option value="40"> 40 </option>
<option value="60"> 60 </option>
</select>
</div>
</div>
<button class="btn: id="btnRemove:> <b> Remove Product </b> </button> <br>
</div>
</fieldset>
</form>
<script>
const btnAdd = document.querySelector('#btnAdd');
const btnRemove = document.querySelector('#btnRemove');
const selectbox = document.querySelector('#list');
const priceselectbox = document.querySelector('#price-list');
const name = document.querySelector('#name');
const price = document.querySelector('#price');
var prod =
btnAdd.onclick = (e) => {
e.preventDefault();
if (name.value == '') {
alert('please enter the name.');
return;
}
if (price.value == '') {
alert('please enter the price.');
return;
}
const option = new Option(name.value, name.value);
const optionprice = new Option(price.value, price.value);
selectbox.add(option, undefined);
priceselectbox.add(optionprice, undefined);
name.value='';
price.value='';
name.focus();
}
btnRemove.onclick = (e) => {
e.preventDefault();
let selected = [];
for(let i = 0; i < selectbox.options.length; i++) {
select[i] = selectbox.options[i].selected;
}
const noneSelected = selected.every(x => !x);
if (noneSelected) {
for (let i = 0; i <priceselectbox.options.length; o++) {
selected[i] = priceselectbox.options[i].selected;
}
}
let index = selectbox.options.length;
while (index--) {
if (selected[index]) {
selectbox.remove(index);
priceselectbox.remove(index);
}
};
}
</script>
</body>
</html>
Comments
Post a Comment