4

I would like to create a DIV element via Javascript, but it should have a onMouseOver effect.

so I know what the tags look like in HTML:

<div onMouseOver="doSth()" onMouseOut="doSthElse()"> </div>

and I know how to create my DIV:

var myDiv= document.createElement("div");
//style settings
document.body.appendChild(myDiv);

but how do I create the effect in Javascript code?

Yaje
  • 2,753
  • 18
  • 32
user2078872
  • 1,507
  • 3
  • 12
  • 16

7 Answers7

11

Without jQuery, this is what you want:

var myDiv = document.createElement('div');

myDiv.onmouseout  = doSth;
myDiv.onmouseover = doSthElse;
// with doSth & doSthElse being functions you defined somewhere else already
// otherwise you can assign a function here:
// myDiv.onmouseout = function(){};

document.body.appendChild( myDiv );
christian314159
  • 639
  • 3
  • 6
4

Use pure Javascript EventTarget.addEventListener

var myDiv= document.createElement("div");
myDiv.addEventListener("mouseover", mouseOver, false);
myDiv.addEventListener("mouseout", mouseOut, false);
document.body.appendChild(myDiv);
function mouseOver()
{  
   //do something
}

function mouseOut()
{  
   //do something
}
Gildas.Tambo
  • 22,173
  • 7
  • 50
  • 78
2

Use jQuery .hover():

$('#myDiv').hover(doSth, doSthElse);
martynas
  • 12,120
  • 3
  • 55
  • 60
2

If you just want to display some other element or change the style of the div, you can also use a CSS class:

var div = document.createElement('div');
div.className = "hoverEffect";

Then you can style the the div using the CSS :hover selector. For example:

div.hoverEffect:hover {
    color:#fff;
}

div.hoverEffect child {
    display:none;
}

div.hoverEffect:hover child {
    display:block;
}
zliw
  • 976
  • 10
  • 10
1

I suggest to use .attr() jquery:

$(myDiv).attr("onMouseOver","doSth()");
$(myDiv).attr("onMouseOut","doSthElse()");
Alex Char
  • 32,879
  • 9
  • 49
  • 70
1

You can pass all calls function on attributes looks like this:

$('#idColor').attr("onMouseOver","showStone('"+arr[i]+"')");

For example:

var arr = new Array();
arr.push("granite-alaskawhite.jpg");
arr.push("granite-bordeauxriver.jpg");
arr.push("granite-copenhagen.jpg");

for(var i = 0; i < arr.length; i++ ) {      
    var img = document.createElement('img');
    var idColor = 'color' + i;
    img.id = idColor;
    img.src = "assets/images/projects3d/colors/" + arr[i];
    img.className = 'proj-items';

    $("#colors").append(img);

    //Call functions
    $('#' + idColor).attr("onMouseOut","hideStone()");
    $('#' + idColor).attr("onMouseOver","showStone('"+arr[i]+"')");

}


function hideStone() {
    $("#p3ddesc").html( "" );
    $('#p3ddesc').attr("class","p3ddesc");
}

function showStone( stoneTxt ) {
    $("#p3ddesc").html( stoneTxt );
    $('#p3ddesc').attr("class","p3ddesc-active");
}
Claudio Silva
  • 3,743
  • 1
  • 26
  • 27
1

One option without using JQuery, is setAttribute(). Unlike some other options, it also allows you to set a parameter in your function call. For example, passing the parameter this in the case that your function needs to alter the calling element.

var myDiv= document.createElement("div");
myDiv.setAttribute("onmouseover", "myFunction(this)")
document.body.appendChild(myDiv);
Broper
  • 2,000
  • 1
  • 14
  • 15