Delete button works only for last item - javascript

I just started working with Javascript and I am trying to make my first "Todo App".
The problem is, that my delete button which should be related to specific div is deleting only last div.
To better understaing check out my code on Codepen:
https://codepen.io/anon/pen/QVPxmG
or here:
var books = ["Bang-1","Bang-2","Bang-3","Bang-4"];
var wrapper = document.querySelector(".wrapper");
var element_div = document.querySelector(".element_div");
var load_button = document.querySelector(".load");
load_button.addEventListener("click", function(){
for(var x=0;x<books.length;x++){
var div = document.createElement("div");
div.setAttribute("class","element_div " + "element_div"+x);
wrapper.appendChild(div);
var element = document.createElement("p");
div.appendChild(element);
element.setAttribute("class", "element"+x);
element.innerHTML = books[x];
var del = document.createElement("button");
del.setAttribute("class", "delete"+x);
div.appendChild(del);
del.innerHTML = 'Delete';
del.addEventListener("click", function(){
div.remove();
},false);
}
},false);
var clear = document.querySelector(".clear");
clear.addEventListener("click", function(){
wrapper.innerHTML = "";
},false);
What Should I change to delete proper div?
Thanks, Mike.

The problem in your case come because of closure,you have declared all your variables using var which will belong to the functional scope and hence when you click on delete, the div that is deleted is the last div since that is what div variable points to after the for loop iteration.
Changing everything to let will work, since let is block scoped and the declaration will be limited to within the for loop
for(let x=0;x<books.length;x++){
let div = document.createElement("div");
div.setAttribute("class","element_div " + "element_div"+x);
wrapper.appendChild(div);
let element = document.createElement("p");
div.appendChild(element);
element.setAttribute("class", "element"+x);
element.innerHTML = books[x];
let del = document.createElement("button");
del.setAttribute("class", "delete"+x);
div.appendChild(del);
del.innerHTML = 'Delete';
del.addEventListener("click", function(){
div.remove();
},false);
}
},false);
Working codepen

You have to use "this" instead of "div" in click event. Something like this:
this.parentElement.remove();

div will have the content after for loop ends. You should capture the correct div for every iteration like below, using pure javascript's immediately invoking function
var books = ["Bang-1", "Bang-2", "Bang-3", "Bang-4"];
var wrapper = document.querySelector(".wrapper");
var load_button = document.querySelector(".load");
load_button.addEventListener("click", function() {
for (var x = 0; x < books.length; x++) {
var div = document.createElement("div");
(function(div) { // Immediately invoking function - IIFE
div.setAttribute("class", "element_div " + "element_div" + x);
wrapper.appendChild(div);
var element = document.createElement("p");
div.appendChild(element);
element.setAttribute("class", "element" + x);
element.innerHTML = books[x];
var del = document.createElement("button");
del.setAttribute("class", "delete" + x);
div.appendChild(del);
del.innerHTML = 'Delete';
del.addEventListener("click", function() {
div.remove();
}, false);
})(div);
}
}, false);
var clear = document.querySelector(".clear");
clear.addEventListener("click", function() {
wrapper.innerHTML = "";
}, false);
* {
margin: 0;
padding: 0;
}
.container {
width: 300px;
height: 250px;
position: relative;
}
.container .wrapper {
width: 300px;
height: 200px;
padding: 10px;
background: aqua;
position: relative;
}
.container .wrapper .element_div {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
}
.container .load {
position: absolute;
bottom: 0px;
}
.container .clear {
position: absolute;
bottom: 0px;
right: 10px;
}
<div class="container">
<div class="wrapper" id="wrapper">
<div class="element_div">
<p class="element">Bang</p>
<button class="delete">Delete</button>
</div>
</div>
<button class="load">LOAD</button>
<button class="clear">CLLEAR</button>
</div>

Related

Appending JS to the HTML

I am trying to append my variables 'showName' and 'showDescription' to the 'results' div object. I have tried to add them in using 'innerHTML' but I just get the description shown. I have tried making additional divs to put INSIDE the 'results' div but that didn't work either.
I want the 'showName' to appear above the 'showDescription in the div.
I am challenging myself to not use JQuery so that is not a viable option.
code:
document.querySelector('.search').addEventListener('keypress', function(e){//On button click of enter, get the value of the search bar and concatanate it to the end of the url
if(e.key==='Enter'){
var query = document.getElementById('main').value;
var url = fetch("http://api.tvmaze.com/search/shows?q="+query) //use fetch to get the data from the url, THEN convert it to json THEN console.log the data.
.then(response => response.json())
.then(data => {
console.log(data)
var domObject = document.createElement('div')
domObject.id="myDiv";
domObject.style.width="800px";
domObject.style.height="5000px";
domObject.style.display="flex";
domObject.style.flexDirection="column";
domObject.style.margin="auto";
domObject.style.borderRadius="30px";
domObject.style.background="";
document.body.appendChild(domObject);
for (var i = 0; i < data.length; i++) { //for all the items returned, loop through each one and show the name of the show and the dsescription of the show.
var showName = data[i].show.name;
//console.log(showName);
var showDescription = data[i].show.summary
//console.log(showDescription);
var results = document.createElement('div')
results.id="myResults";
results.style.width="600px"
results.style.height="400px";
results.style.background="white";
results.style.margin="auto";
results.style.borderRadius="30px";
results.style.fontFamily="Poppins"
results.style.display="flex";
results.style.flexDirection="column";
results.innerHTML=showName;
results.innerHTML=showDescription;
document.getElementById("myDiv").appendChild(results);
}
})
}
});
document.querySelector('.search').addEventListener('keydown', function(o){
if(o.key==='Backspace'){
location.reload();
}
});
result of searching in 'car'
results.innerHTML = showName;
results.innerHTML = showDescription;
With this you are overwriting showName with showDescription.
What you need to do is concatenate with +=.
Also, it will be much easier to replace this:
domObject.style.width = "800px";
domObject.style.height = "5000px";
domObject.style.display = "flex";
domObject.style.flexDirection = "column";
domObject.style.margin = "auto";
domObject.style.borderRadius = "30px";
domObject.style.background = "";
with domObject.classList.add('some-class');
and CSS will be:
.some-class {
width: 800px;
height: 500px;
// etc...
}
Moved your code to a working example.
Note: because of authors styles, it is only possible to run snippet in fullscreen. =)
const dosearch = () => {
var query = document.getElementById('main').value;
var url = fetch("https://api.tvmaze.com/search/shows?q=" + query)
.then(response => response.json())
.then(data => {
const myDiv = document.getElementById("myDiv");
myDiv.innerHTML = ''; // <---- this is for testing
for (var i = 0; i < data.length; i++) {
var showName = data[i].show.name;
var showDescription = data[i].show.summary
var results = document.createElement('div');
results.className = 'myResults';
var header = document.createElement('h2');
header.innerHTML = showName;
results.appendChild(header);
var desc = document.createElement('div');
desc.innerHTML = showDescription;
results.appendChild(desc);
myDiv.appendChild(results);
}
});
}
document.querySelector('.search').addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
dosearch();
}
});
#myDiv {
width: 800px;
height: 5000px;
display: flex;
flex-direction: column;
margin: auto;
border-radius: 30px;
background: black;
}
.myResults {
width: 600px;
height: 400px;
background: white;
margin: auto;
border-radius: 30px;
font-family: Poppins;
display: flex;
flex-direction: column;
}
.myResults p, .myResults h2 {
margin: 1em;
}
<input type="text" id="main" class="search" style="margin-bottom: 4px" value="Car" /><button onclick="dosearch()">Go</button>
<div id="myDiv"></div>

Javascript: set the timer to run different link every minute

In this program I created object and instances of it and stored those instances in array. I am retrieving array index by getting the modulo of minutes with the length of the array. I am trying to display link in div tag and I should be able to see different link every minute. Upon clicking link it should show different url. and set the timer to run every minute. For that I have created setInterval(). but it doesn't seem to work. Can anyone help me?
code:
<!DOCTYPE html>
<html>
<head>
<style>
.myDiv{
width: 750px;
height: 150px;
border: #CED8BC 3px solid;
border-radius: 20px;
position: absolute;
top: 30%;
left: 20%;
}
div p {
text-align: center;
font-family: monospace;
font-size: 30px;
}
a{
text-align: center;
text-decoration: none;
color: #3bb570;
}
a:hover{
color:#efa5db
}
</style>
<title>lab15</title>
</head>
<body background="lab15_images/pink.jpg">
<div class="myDiv" id="div">
<p> Click on the link to see a website. </p>
<!-- <p><b> </b></p> -->
<p id="link"> </p>
</div>
<script>
function site(the_url, website_name) {
this.the_url = the_url;
this.website_name = website_name;
}
var myWebsite = new site("http://www.cnn.com/", "CNN");
var myWebsite2 = new site("http://www.bbc.com/news", "BBC");
var myWebsite3 = new site("http://www.foxnews.com/", "FOX NEWS");
var myWebsite4 = new site("http://abcnews.go.com/", "ABC NEWS");
var myWebsite5 = new site("https://www.cbsnews.com/", "CBS NEWS");
var instances = new Array(myWebsite, myWebsite2, myWebsite3, myWebsite4, myWebsite5);
setInterval(changeLink, 60000);
function changeLink() {
var n = new Date().getMinutes();
var index = n % instances.length
var site = instances[index]
var counter = 0;
var ele = document.getElementbyId("link");
ele.innerHTML = instances[counter];
counter++;
if(counter >= instances.length) {
counter = 0;
}
var a = document.createElement('a');
var myDiv = document.getElementbyId("div");
a.href = site.the_url;
a.innerHTML = site.website_name
myDiv.appendChild(a);
document.body.appendChild(myDiv);
}
</script>
</body>
</html>
Fixed your semicolons and getElementById typos. Here is the working code.
function site(the_url, website_name) {
this.the_url = the_url;
this.website_name = website_name;
}
var myWebsite = new site("http://www.cnn.com/", "CNN");
var myWebsite2 = new site("http://www.bbc.com/news", "BBC");
var myWebsite3 = new site("http://www.foxnews.com/", "FOX NEWS");
var myWebsite4 = new site("http://abcnews.go.com/", "ABC NEWS");
var myWebsite5 = new site("https://www.cbsnews.com/", "CBS NEWS");
var instances = new Array(myWebsite, myWebsite2, myWebsite3, myWebsite4, myWebsite5);
// call changeLink once to display on page load
changeLink();
// interval changed to 3 seconds so that you dont need to wait a minute for the result
setInterval(changeLink, 3000);
function changeLink() {
var n = new Date().getMinutes();
var index = n % instances.length;
var site = instances[index];
var counter = 0;
var ele = document.getElementById("link");
counter++;
if (counter >= instances.length) {
counter = 0;
}
var a = document.createElement('a');
var myDiv = document.getElementById("div");
a.href = site.the_url;
a.innerHTML = site.website_name;
ele.innerHTML = '';
ele.appendChild(a);
}
.myDiv {
width: 750px;
height: 150px;
border: #CED8BC 3px solid;
border-radius: 20px;
position: absolute;
top: 30%;
left: 20%;
}
div p {
text-align: center;
font-family: monospace;
font-size: 30px;
}
a {
text-align: center;
text-decoration: none;
color: #3bb570;
}
a:hover {
color: #efa5db
}
<!DOCTYPE html>
<html>
<head>
<style>
</style>
<title>lab15</title>
</head>
<body background="lab15_images/pink.jpg">
<div class="myDiv" id="div">
<p> Click on the link to see a website. </p>
<!-- <p><b> </b></p> -->
<p id="link"> </p>
</div>
</body>
</html>
Move var counter = 0; out of the function.
Also, as #guest271314 pointed, you have to capitialize the by in:
var ele = document.getElementById("link");
/* ... */
var myDiv = document.getElementbyId("div");
Final code:
var counter = 0;
function changeLink() {
var n = new Date().getMinutes();
var index = n % instances.length
var site = instances[index]
//var counter = 0;
var ele = document.getElementById("link"); //Capitalize By
ele.innerHTML = instances[counter];
counter++;
if (counter >= instances.length) {
counter = 0;
}
var a = document.createElement('a');
var myDiv = document.getElementById("div"); //Capitalize By
a.href = site.the_url;
a.innerHTML = site.website_name
myDiv.appendChild(a);
document.body.appendChild(myDiv);
}

Getting different id for 24 buttons, made with a for loop

I have made 24 buttons with a for loop, and want button from number 1 to 24 to give a message like this.
"you clicked on button 1"
"you clicked on button 2" and so on.
I have been able to split the 3 first buttons so they say "button 1" "2" "3", but that is done by 3 if statements, which means i would need 23-24 ish if statements to get them all to do as I want. That's not a very efficient way to do it.
Is there a good way to get the button id to add +1 after "knapp" every time the loop runs ? something like this element.id = "knapp" + 1; < so the id become knapp1, knapp2, knapp3 as the loop keep running 24 times ?
html:
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<script src="Assignment06.js"></script>
<style>
h1 {
text-align: center;
}
div {
background-color: forestgreen;
border: solid 1px #000;
display: inline-block;
width: 100px;
height: 100px;
padding: 10px
}
#panel {
width: 610px;
margin: 0 auto;
}
</style>
</head>
<body>
<h1>Assignment06</h1>
<p id = "panel"></p>
</body>
</html>
Javascript:
function dag(){
knapp = window.alert("Du trykket knapp 1");
}
function dag2(){
window.alert("Du trykket knapp 2");
}
function dag3(){
window.alert("Du trykket knapp 3");
}
function init(){
knapper();
}
function knapper(){
for (var antall = 1; antall <= 24; antall++){
if(antall == 1){
var element = document.createElement("div");
element.innerHTML = antall;
element.id = "knapp";
knapp = element.addEventListener("click", dag);
element.type = "div";
var panel = document.getElementById("panel");
panel.appendChild(element);
}
else if (antall == 2){
var element = document.createElement("div");
element.innerHTML = antall;
element.id = "knapp2";
knapp2 = element.addEventListener("click", dag2);
element.type = "div";
var panel = document.getElementById("panel");
panel.appendChild(element);
}
else{
var element = document.createElement("div");
element.innerHTML = antall;
element.id = "knapp3";
knapp3 = element.addEventListener("click", dag3);
element.type = "div";
var panel = document.getElementById("panel");
panel.appendChild(element);
}
}
}
window.onload = init;
You can save the id in the dataset of the <div /> element.
function knapper() {
var panel = document.getElementById("panel");
for (var antall = 1; antall <= 10; antall++) {
var element = document.createElement("div");
element.innerHTML = antall;
element.dataset.id = antall;
element.addEventListener("click", dag);
panel.appendChild(element);
}
}
function dag(evt) {
alert(evt.target.dataset.id);
}
window.onload = knapper;
#panel div {
width: 50px;
height: 50px;
border: solid 1px black;
float: left;
}
<div id="panel"></div>
To directly answer your question without suggestions:
You already have a counter in the for loop (antall). You can just use that variable and concatenate it on the end of the string that you're using as an id.
element.id = "knapp" + antall;

Event handling onclick Body response in every object

There is a pattern game in which the user is supposed to find 1 missed picture from right side while the location is shown on left, then if the user guessed correctly they will go to the next level.
My problem is that, even though I clicked on correct picture, the popup for wrong choice is appearing after the first iteration.
I tried to define body as the initial child but still popup is displaying regardless of correct or wrong choice.
var numberOfFaces = 5;
var theLeftSide = document.getElementById("leftSide");
var theRightSide = document.getElementById("rightSide");
var theBody = document.getElementsByTagName("body")[0];
function generateFaces() {
for (var i = 0; i < numberOfFaces; i++) {
var createImg = document.createElement("IMG");
var randPositionTop = Math.floor(Math.random() * 400);
var randPositionleft = Math.floor(Math.random() * 400);
createImg.setAttribute("src", "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png");
createImg.style.top = randPositionTop + "px";
createImg.style.left = randPositionleft + "px";
theLeftSide.appendChild(createImg);
}
var leftSideImages = theLeftSide.cloneNode(true);
leftSideImages.removeChild(leftSideImages.lastChild);
theRightSide.appendChild(leftSideImages);
theLeftSide.lastChild.onclick =
function nextLevel(event) {
event.stopPropagation;
numberOfFaces += 5;
alert("Next level!");
while (theLeftSide.firstChild) {
theLeftSide.removeChild(theLeftSide.firstChild);
}
generateFaces();
}
theBody.onclick =
function gameOver() {
theBody.onclick = null;
theLeftSide.lastChild.onclick = null;
alert("Game Over!");
};
}
#leftSide {
background-color: red;
position: absolute;
width: 500px;
height: 500px;
left: 50px;
}
#rightSide {
position: absolute;
left: 600px;
width: 500px;
height: 500px;
--border-left: solid 10px;
}
img {
position: absolute;
width: 50px;
height: 50px;
}
<link rel="stylesheet" href="style.css">
<body onload="generateFaces()">
<h3>Matching Game</h3>
<h5>Please choose missed smile !</h5>
<div id="leftSide">
</div>
<div id="rightSide">
</div>
<script src="jsCode.js"></script>
</body>
Updated your fiddle. Is this how you want it?
https://jsfiddle.net/ychq5rkm/12/
Specifically it uses document.ready() in the js instead of in the onload in the html. As was mentioned in the comments, you needed to actually call event.stopPropegation() because it was continuing up to the body each time and ending your game.

DIV POP UP containing clickable images using javascript

I have an image button which changes images with a click of a button . This is the following code
function changeImg(thisImg)
{
var altImg = "http://ww1.prweb.com/prfiles/2011/10/12/8875514/star_white.jpg";
var tmpImg = null;
function changeImg(thisImg) {
tmpImg = thisImg.src;
thisImg.src = altImg;
altImg = tmpImg;
}
this is the fiddle http://jsfiddle.net/pUbrv/ which I did previously for clickable images
<img alt="" src="http://www.gettyicons.com/free-icons/136/stars/png/256/star_gold_256.png" id="imgClickAndChange" onclick="changeImg(this)" />
Instead of just chnaging the image by click of a button , I want to pop up a div which contains clickable images after I click the image in the div , the image choud change . Can please anybody help me?
Here is simple Javascript solution.
DEMO
HTML
<body>
<img alt="" src="http://www.gettyicons.com/free-icons/136/stars/png/256/star_gold_256.png" id="imgClickAndChange" onclick="myfunction(this)" />
</body>
SCRIPT
var altImg = "http://ww1.prweb.com/prfiles/2011/10/12/8875514/star_white.jpg";
var tmpImg = null;
function changeImg(thisImg) {
tmpImg = thisImg.src;
thisImg.src = altImg;
altImg = tmpImg;
}
function myfunction(ele) {
var pop=new myPop();
pop.popOut(ele);
}
function myPop() {
this.square = null;
this.overdiv = null;
this.popOut = function(ele) {
tmpImg = ele.src;
//filter:alpha(opacity=25);-moz-opacity:.25;opacity:.25;
this.overdiv = document.createElement("div");
this.overdiv.className = "overdiv";
this.square = document.createElement("div");
this.square.className = "square";
this.square.Code = this;
var msg = document.createElement("div");
msg.className = "msg";
msg.innerHTML = '<img alt="" src="'+altImg+'" id="imgClickAndChange" />';
altImg = tmpImg;
this.square.appendChild(msg);
var closebtn = document.createElement("button");
closebtn.onclick = function() {
this.parentNode.Code.popIn();
}
closebtn.innerHTML = "Close";
this.square.appendChild(closebtn);
document.body.appendChild(this.overdiv);
document.body.appendChild(this.square);
}
this.popIn = function() {
if (this.square != null) {
document.body.removeChild(this.square);
this.square = null;
}
if (this.overdiv != null) {
document.body.removeChild(this.overdiv);
this.overdiv = null;
}
}
}
CSS
div.overdiv { filter: alpha(opacity=75);
-moz-opacity: .75;
opacity: .75;
background-color: #c0c0c0;
position: absolute;
top: 0px;
left: 0px;
width: 100%; height: 100%; }
div.square { position: absolute;
top: 200px;
left: 200px;
background-color: Menu;
border: #f9f9f9;
height: 200px;
width: 300px; }
div.square div.msg { color: #3e6bc2;
font-size: 15px;
padding: 15px; }
Pollisbly the jQuery UI dialog can help you.
Since the changing of images you have already implemented, you just need to put the images in the dialog.
Also you may like the modal functionality of the dialog.
Edit
If you don't want to use jQuery UI, then here is what you can do:
Create a new div.
Initially make it hidden or set its display property to none.
When user clicks on the image, make this div visible.
Set its position to absolute and set its left and top corrdinates as you want.
Add the image this div,
and finally add the event handler to this div for changing the images.

Categories

Resources