-6

assume I have this sample code:

<div class="class1">
  ... random content
</div>

Now all I want to do is just have a Javascript for on hover of that div, I need to add a css attribute:

background-color:#ffffe0;
border:1px solid #bfbfbf;

Thats really all I wish to achieve, however I wish to do this without jQuery.

EDIT: I don't really need a CSS solution as my application runs in quirks mode, which means it is not compatible in IE.

nicael
  • 18,550
  • 13
  • 57
  • 90
  • 4
    Why not to use CSS `:hover` pseudo-class? – VisioN May 29 '14 at 14:55
  • 1
    I already have, however my application doesnt support that for IE as it is old and running quirks mode. I need javascript support for multi browser compatibility – user3378366 May 29 '14 at 14:56
  • While I'm sure you will get a solution without any effort on your behalf, it is better to post [**your code/css/etc**](http://whathaveyoutried.com) you have tried and might have issues with so we can have a look and help to see why it doesn't work. Also, adding a [**jsFiddle**](http://jsFiddle.net) (or similar) that demonstrates any issues you might have is helpful too. – Nope May 29 '14 at 14:58
  • The duplicate is not correct. Should be closed for this one instead: http://stackoverflow.com/questions/608788/css-hover-vs-javascript-mouseover?rq=1 – Artyom Neustroev May 29 '14 at 14:59
  • @ArtyomNeustroev Yours is also not correct, as it does not provide a solution for pure JS. – VisioN May 29 '14 at 14:59
  • Ok, I don't have the time right now to find the exact JS dupe for this question. Reopened. @VisioN do you? :| – Roko C. Buljan May 29 '14 at 15:01
  • @RokoC.Buljan Or mabey instead of spending time trying to close my thread down, you could spend half that time and answer my question? would be appreciated. – user3378366 May 29 '14 at 15:02
  • @user3378366 ok I'll do my best, but I'm sure there's a dupe somewhere ;) – Roko C. Buljan May 29 '14 at 15:02
  • @FrançoisWahl That thread has no relation to javascript what so ever. Can i ask what is wrong with this community? your all trying so hard to get my thread closed down instead of helping me out. Really bad impression to me. – user3378366 May 29 '14 at 15:04
  • 1
    @user3378366: `Or mabey instead of spending time trying to close my thread down, you could spend half that time and answer my question?`....what have you tried so far --> [**http://whathaveyoutried.com**](http://whathaveyoutried.com)? – Nope May 29 '14 at 15:05
  • @FrançoisWahl I have tried researching online to find and learn about this. – user3378366 May 29 '14 at 15:06
  • 1
    @user3378366: More research: [**ie6-hover-issue**](http://stackoverflow.com/questions/2571073/ie6-hover-issue) - points to `whatever:hover` --> [**http://peterned.home.xs4all.nl/csshover.html**](http://peterned.home.xs4all.nl/csshover.html) – Nope May 29 '14 at 15:09
  • I edited. See my answer. – nicael May 29 '14 at 15:14

1 Answers1

1

Hope this helps:

http://jsbin.com/podip/2/edit

// IE6 does not support getElementsByClassName so...

function getElementsByClassName(className) { // http://stackoverflow.com/questions/6584635/getelementsbyclassname-doesnt-work-in-ie6
  var elz = [];
  var elements = document.getElementsByTagName("*");
  for (var i = 0; i < elements.length; i++) {
    var names = elements[i].className.split(' ');
    for (var j = 0; j < names.length; j++) {
      if (names[j] == className) elz.push(elements[i]);
    }
  }
  return elz;
}

// I like to use this nice function of mine to play with styles:

function css( el, obj ){
  if(typeof obj == "object" ){
    for(var k in obj){
      el.style[k] = obj[k];
   }
  } // else, it's not an {} so you might want to handle 'string' here... 
}

var $cl1 = getElementsByClassName("class1"); // Let's use now our fn

function hoverEvents( element ){
  element.onmouseenter = function(){
    css( element, {
      backgroundColor: "#ffffe0",
      border : "1px solid #bfbfbf"
    });
  };
  element.onmouseleave = function(){
    css( element, {
      backgroundColor: "transparent",
      border : "none"
    });
  };
}

// Set hover states by passing the element to our hoverEvents fn

for(var i=0; i<$cl1.length; i++){
  hoverEvents( $cl1[i] ); // bound listeners to all our elements
}
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313