Change div background on click and on mouse over - javascript

I am trying to change picture in div on mouse over and on click using JS. Like that:
var favs = document.getElementsByClassName("fav-wrapper");
for (var i = 0; i < favs.length; i++) {
favs[i].innerHTML = '<img src="images/favorite.png" />';
favs[i].onMouseOver = function () {
favs[i].innerHTML = "<img src='images/favorite_hover.png' />";
}
favs[i].onClick = function () {
favs[i].innerHTML = "<img src='images/favorite_on.png' />";
};
}
But for some reason it won't work. What am I doing wrong?

Try lowercase event handlers and actually you need a closure to have the [i] work inside the loop. I prefer using this in your case.
var favs = document.getElementsByClassName("fav-wrapper");
for (var i = 0; i < favs.length; i++) {
favs[i].innerHTML = '<img src="images/favorite.png" />';
favs[i].onmouseover = function () {
this.innerHTML = "<img src='images/favorite_hover.png' />";
}
favs[i].onclick = function () {
this.innerHTML = "<img src='images/favorite_on.png' />";
}
}
but why not just change the src of the images?
var favs = document.getElementsByClassName("fav-wrapper");
for (var i = 0; i < favs.length; i++) {
var fav=favs[i];
fav.getElementsByTagName("img")[0].src="images/favorite.png";
fav.onmouseover = function () {
this.getElementsByTagName("img")[0].src="images/favorite_hover.png";
}
fav.onmouseout = function () {
this.getElementsByTagName("img")[0].src="images/favorite.png";
}
fav.onclick = function () {
this.getElementsByTagName("img")[0].src="images/favorite_on.png";
}
}
you could do all this with CSS by the way

you can try this method using pure css changing background on hover and on click
.image{width:500px;height:500px;background-image: url("http://www.toolsformoney.com/financial_software_demos.jpg");background-repeat: no-repeat;}
.image:hover{background-image: url("http://www.hostpph.com/blog/wp-content/uploads/2012/06/free-bookie-software-demo-large.jpg");background-repeat: no-repeat;}
.image:focus{background-image: url("https://www.arxan.com/wp-content/uploads/assets1/images/demo.png");background-repeat: no-repeat;outline: 0;transition:0s;}
<div class="image" tabindex="0">
</div>

Related

.addEventListener - long list

I found few answer on my issue but probably I'm not so experienced to processes it to my case.
I have list of items generated to .html
<div id="grid">
by JavaScript
var div_block = "";
for (i = 0; i < 70; i++) {
div_block = div_block + '<div id="c' + i + '" class="card"></div>';
}
document.getElementById("grid").innerHTML = div_block;
for (i = 0; i < 70; i++) {
'var c' + i + ' = document.getElementById(c' + i + ');'
}
and it works fine.
I want to chose .addEventListner method to chose proper element but for 70 and more elements code:
c0.addEventListener("click", function () {revealCard(0);});
c1.addEventListener("click", function () {revealCard(1);});
...
cn.addEventListener("click", function () {revealCard(n);});
is huge and not elegant. Method I've tried didn't work
for (i = 0; i < 70; i++) {
'c'+i+'.addEventListener("click", function() { revealCard('+i+'); });'
}
How to build working addEventListener() for this?
Thanks a lot.
The problem you are facing can be solved by using the card class that you add on each of your card. Then, to refer to the right card, you can use the keyword this, which in the context of an addEventListener will refer to whichever DOM element received the click. You also won't need to generate a unique Id for each one of your div, which I think is a big plus.
Your code would look like this:
let div_block = "";
for (i = 0; i < 70; i++) {
div_block = div_block + '<div class="card"></div>';
}
const cards = querySelectorAll(".card");
cards.forEach(card => {
card.addEventListener("click", revealCard)
})
function revealCard(){
// Here, `this` refers to the card that was clicked
// So you can do what you want with it
console.log(this);
}
Slight modification to brk's answer, using a single event listener on the parent that will trigger for the events on the children
var div_block = "";
for (let i = 0; i < 5; i++) {
div_block += `<div data-attr="${i}" id="c${i}" class="card">Hello</div>`;
}
var grid = document.getElementById("grid");
grid.innerHTML = div_block;
grid.addEventListener('click', function (e) {
if (e.target.getAttribute('class') === 'card') {
revealCard(e.target.dataset.attr);
}
});
function revealCard(num) {
console.log(num)
}
<div id='grid'></div>
You can use dataset, that is while creating the dom add a dataset property.
Then use querySelectorAll to get all the div with class card and iterate over it to add event using addEventListener. On click of the element get the dataset value and pass to revealCard function
var div_block = "";
for (let i = 0; i < 5; i++) {
div_block += `<div data-attr="${i}" id="c${i}" class="card">Hello</div>`;
}
document.getElementById("grid").innerHTML = div_block;
document.querySelectorAll('.card').forEach(function(item) {
item.addEventListener('click', function(e) {
revealCard(item.dataset.attr)
})
})
function revealCard(num) {
console.log(num)
}
<div id='grid'></div>
There are multiple ways to do this, but I wouldn't use IDs and I wouldn't bind X event listeners. I would use event delegation and data-* attributes:
Build your list of elements:
const grid = document.getElementById("grid");
for (var i = 0; i < 70; i++) {
const child = document.createElement('div');
child.className = 'card';
child.dataset.index = i;
grid.appendChild(child);
}
Add an event listener to the grid element:
grid.addEventListener('click', function(event) {
let target = event.target;
// traverse up if clicked inside element
while (target.className !== 'card') {
target = target.parentNode;
}
if (target) {
revealCard(target.dataset.index);
}
});
const grid = document.getElementById("grid");
for (var i = 0; i < 70; i++) {
const child = document.createElement('div');
child.className = 'card';
child.dataset.index = i;
child.innerText = `Card ${i}`;
grid.appendChild(child);
}
grid.addEventListener('click', function(event) {
let target = event.target;
// traverse up if clicked inside element
while (target.className !== 'card') {
target = target.parentNode;
}
if (target) {
revealCard(target.dataset.index);
}
});
function revealCard(i) {
console.log(`Card ${i} revealed`);
}
<div id="grid"></div>
I found probably the easiest (shortest) solution:
for(var i=0;i<boardSize;i++){
document.getElementById('c'+i).addEventListener("click",function(){
revealCard(this.id.substring(1));
});
}
What do you thing?

How wrap dynamically created image element using JavaScript

I want display image preview on change event of file input. So I am creating Image element using JavaScript and I want to wrap it inside a div to display a close button. To achieve this I have written following code:
preview(event, imageList) {
let imgLen = event.target.files.length;
if (imgLen > 0) {
for (let i = 0; i < imgLen; i++) {
let myImage = new Image(100, 100);
myImage.src = URL.createObjectURL(event.target.files[i]);
myImage.setAttribute('style', 'margin: 5px');
imageList.appendChild("<div>" + myImage + "</div>");
}
}
}
But I throwing error at the run time.
error
Use this:
var divNode = document.createElement('DIV');
divNode.appendChild(myImage);
imageList.appendChild(divNode);
preview(event, imageList) {
let imgLen =event.target.files.length;
if(imgLen > 0) {
for (let i = 0; i< imgLen; i++) {
let myImage = new Image(100, 100);
myImage.src = URL.createObjectURL(event.target.files[i]);
let div = document.createElement('div');
div.appendChild(myImage);
myImage.setAttribute('style', 'margin: 5px');
imageList.append( myImage);
}
}

Modal doesn't work

I made a modal using jQuery, but it doesn't seem to work properly. I'm calling my images in a javascript function, and the modal is in my window.onload in the same page the images are loaded. I'm loading the images first, but the modal only works if i put the imgs directly in the HTML and if i take everything else from the window.onload. That's how i'm calling all imgs:
function maisGaleria(n){
var galeria = new Array();
var img = document.querySelector("#gallery");
var pic = 0;
if(n == 1){
pic = 4;
}else if(n == 2){
pic = 7;
}else{
pic = 4;
}
img.innerHTML = "<div class='row'>";
img.innerHTML += "<div class='six columns'>";
img.innerHTML += "<h4>Galeria "+n+"</h4>";
for(var i = 0; i < pic; i++){
galeria[i] = "foto"+n+"_"+(i+1)+".jpg";
}
for(var i = 0; i < galeria.length; i++){
img.innerHTML += "<img src='img/"+galeria[i]+"' class='imgs-modal'>";
}
img.innerHTML += "</div>";
img.innerHTML += "</div>";
}
and the window.onload with the modal:
window.onload = function(){
var rdSocial = document.querySelector("#social");
var teste = document.querySelector("#teste");
var fb = '<img src="img/fb.png" value="1" class="img" onclick="redireciona(this);">';
var twt = '<img src="img/twitter.png" value="2" class="img" onclick="redireciona(this);">';
var insta = '<img src="img/insta.png" value="3" class="img" onclick="redireciona(this);">';
rdSocial.innerHTML += fb;
rdSocial.innerHTML += twt;
rdSocial.innerHTML += insta;
var x = location.search.split("?gal=")[1];
y = document.querySelector("#pics");
//console.log(x);
y.onload = maisGaleria(x);
//the modal starts here
$('.imgs-modal').on('click', function(){
var obj = $(this).clone();
console.log(obj);
$("#box_modal").html(obj);
$("#modal").fadeIn(1000);
});
$(".fechar").on('click', function(){
$("#modal").fadeOut(1000);
});
}
#anuseranother there are few errors in you code.
Before calling maisGaleria method in window.onload, you have to declare/define it.
As per your jsfiddle, it is throwing errors like maisGaleria is not defined. So define maisGaleria it before using it.
After fixing this error, another error is "Cannot set property 'onload' of null". There is no #pics element in the dom (y = document.querySelector("#pics")) and you are referencing it and adding onload method to it. Please update these two and let us know exactly how you need a modal with images any example in internet.

Passing an argument to a event listener from dynamically created buttons

I'm dynamically creating 3 buttons. How can I pass an argument tohandlerX?
So basically I want the values in the category Array to be passed on to the handlerX eventListener.
Example:
When myBtn1 is clicked, I want the alert to be "fur_",
When myBtn3 is clicked, I want the alert to be "fas_"
var btns = '';
var category = ["fur_", "fts_", "fas_"];
for (i = 1; i < category.length; i++) {
btns += '<button type="button" class=' + category[i] + ' id= "myBtn' + i + '">.....</button>';
}
var div = document.getElementById('div');
div.innerHTML = btns;
var handlerX = function () {
alert('Clicked'); //get value from the 'category' Array
};
var buttons = div.querySelectorAll('button');
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', handlerX, false);
}
The answers given so far are good and should solve your problem. Just thought I'd add this one because I think it's a solution more in line with what you were asking for: Make your handlerX return a function like so:
var handlerX = function (param) {
return function() {alert(param);};
};
var buttons = div.querySelectorAll('button');
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', handlerX(category[i]), false);
}
Edit: Here's a Fiddle
If you're willing to extract it from the class attribute, then:
var handlerX = function () {
alert(this.getAttribute('class'));
};
Or you better associate it with some data- attribute. For example:
for (i = 1; i < category.length; i++) {
btns += '<button type="button" data-category="' + category[i] + '" class=' + category[i] + ' id= "myBtn' + i + '">.....</button>';
}
Then:
var handlerX = function () {
alert(this.getAttribute('data-category'));
};
See Fiddle
EDIT:
then i would reccomend adding an attibute: data-category="fur_" for example, and access that from your event handler:
this.getAttribute('data-category')
in hadlerX there is a "this" that is the element that was clicked. You can access its getAttribute("class") to get the class EDIT:this, not self
var fragment = document.createDocumentFragment(),
categories = ["fur_", "fts_", "fas_"],
btn;
function onClickBtn() {
alert(this.getAttribute("data-category"));
}
for (var i = 0; i < categories.length; i++) {
btn = document.createElement("button");
btn.id = "myBtn" + string(i);
btn.setAttribute("data-category", category[i]);
btn.addEventListener("click", onClickBtn);
fragment.appendChild(btn);
}
var div = document.getElementById('div');
div.appendChild(fragment);

div not firing onclick event in javascript

my div element is :
<div id="div"></div>
below is javascript.:
function makediv() {
var divmy = document.getElementById("div");
var row ='';
var color='#ccc';
for(var i=0;i<2;i++)
{
divmy.innerHTML += " <div class='dynamic' id='inner"+i+"' onclick=searchFilterations(i)>"+i+"</div> ";
color='black';
}
}
function searchFilterations(e){
alert(e);
}
problem is: searchFilterations() function not firing on click.what is the error?
here is fiddle :
http://jsfiddle.net/cjT4s/4/
Please call makediv function onload event
window.onload = makediv;
function makediv() {
var divmy = document.getElementById("div");
var row ='';
var color='#ccc';
for(var i=0;i<2;i++)
{
// divmy.innerHTML += " <div style='width: 100px;height: 30px;background-color:"+color+" ;' onclick=javascript:alert("+i+")></div> ";
divmy.innerHTML += " <div class='dynamic' id='inner"+i+"' onclick=searchFilteration("+i+")>"+i+"</div> ";
color='black';
}
}
function searchFilteration(i){
alert(i);
}
FIDDLE DEMO
Try this with window.searchFilteration = function (i) { ... }:
makediv();
function makediv() {
var divmy = document.getElementById("div");
var row ='';
var color='#ccc';
for(var i=0;i<2;i++)
{
divmy.innerHTML += " <div class='dynamic' id='inner"+i+"' onclick='searchFilteration("+i+")'>"+i+"</div> ";
color='black';
}
}
window.searchFilteration = function (i){
alert(i);
}
FIDDLE DEMO

Categories