Add items in the list and click the remove items button and add other items instead

Asked

Viewed 48 times

0

I have tried it in several ways and so far without an exact success, for now what I want in the application is that I type the number in the input, and when I click enter, the numbers are aligned in the line, (preferably I wanted it to be more dynamic, without having to press enter, as it is in the first code commented, but I think it would be more difficult to solve), when clicking enter, I want you to take the old numbers from the line and keep the new typed, but I could only let it happen when I click a second time in Enter, the first time removes the elements and the second puts the new.

let line = document.getElementById('line-one');
let number = document.getElementById('number');
let space = document.getElementById('space');
let submitBtn = document.getElementById('button-js');

//number.addEventListener('input', (e) => {
//    let arr = [];
//    
//    for (let i  = 0; i < number.value; i++) {
//        arr.push(i + 1);
//        let lineNumber = document.createElement('div');
//        lineNumber.classList.add('line-number');
//        lineNumber.innerHTML = arr[i];
//        
//        line.appendChild(lineNumber);
//        
//        if (number.value.isUndefined == true) {
//            while (line.hasChildNodes()) {
//                line.removeChild(line.lastChild);
//            }
//            arr.splice(0, arr.length);
//        }
//		
//		console.log(typeof(number.value))
//        
//    }
//    
//    console.log(typeof(i));    
//});

let count = 0;

submitBtn.addEventListener('click', e => {
	e.preventDefault();
	let arr = [];
	count++;
	
//	if (!line.hasChildNodes()) {
//		for (let i = 0; i < number.value; i++) {
//			arr.push(i + 1);
//
//			let lineNumber = document.createElement('div');
//			lineNumber.classList.add('line-number');
//			lineNumber.innerHTML = arr[i];
//
//			line.appendChild(lineNumber);
//
//		}
//
//	} else {
//		while (line.hasChildNodes()) {
//			line.removeChild(line.lastChild);
//		}
//		arr.splice(0, arr.lenght);
//	}
	
	do {
		for (let i = 0; i < number.value; i++) {
			arr.push(i + 1);

			let lineNumber = document.createElement('div');
			lineNumber.classList.add('line-number');
			lineNumber.innerHTML = arr[i];

			line.appendChild(lineNumber);

		}
	} while (!line.hasChildNodes());
	
	if (count == 2) {
		while (line.hasChildNodes()) {
			line.removeChild(line.lastChild);
			count = 0;
		} 
                arr.splice(0, arr.length);
	}

	console.log(arr, count);

});
#wrapper {
    margin: 0;
    padding: 0;
}

#wrapper .inputs {
    display: -webkit-flex;
    display: -moz-flex;
    display: -ms-flex;
    display: -o-flex;
    display: flex;
    -webkit-flex-direction: column;
    -moz-flex-direction: column;
    -ms-flex-direction: column;
    -o-flex-direction: column;
    flex-direction: column;
    align-items: flex-start;
}

#wrapper input[type=number]{
    margin-bottom: 20px;
}

#wrapper a.button {
	margin: 20px 0;
	background: #fff;
	color: #000;
	text-decoration: none;
	padding: 6px 20px;
	border: 1px solid #111;
	border-radius: 3px;
}

#wrapper a.button:hover {
	border-color: #666;
	color: #666;
}

#wrapper #line-one {
    border-top: 1px solid #111;
    padding-top: 10px;
    width: 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: -webkit-flex;
    display: -moz-flex;
    display: -ms-flex;
    display: -o-flex;
    display: flex;
    justify-content: space-between;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="css/main.css">
    <title>Group of Symmetries</title>
</head>

<body>

    <div id="wrapper">

        <div class="inputs">
            <label for="number">Number quantity</label>
            <input id="number" type="text">

            <label for="space">Space</label>
            <input id="space" type="text">
            
            <a href="" id="button-js" class="button">Enter</a>
        </div>

        <div id="line-one">
            
        </div>

    </div>

<script src="js/main.js"></script>
</body>

</html>

The last active code is with Do While, which I had never used, only came to mind as a test, and I’m learning JS, so this project itself, I mean, I already know there must be 10 better and cleaner ways to do what I want.

To test the best application here https://jsfiddle.net/eqmkne3u/

  • "for now what I want in the application is that I enter the number in the input"... in your code has 2 inputs. Could explain better please?

  • Sorry I didn’t explain it right, but basically don’t worry about the second input, the "space" for now is there for me to implement later, it doesn’t do anything yet.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.