waa1990
waa1990

Reputation: 2413

Getting a div's information when I mouse over it

I want to be able to mouse over a <div> and use JavaScript to get the information (for example the id) and store it in a variable.

What would be the most efficient way to make this happen?

Upvotes: 0

Views: 3343

Answers (8)

Jared Farrish
Jared Farrish

Reputation: 49198

Here is a simple example:

<div id="test">Hello world!</div>

$('#test').mouseover(function(){
    var test = $(this);
    console.log(test);
});

http://jsfiddle.net/4uLzf/

Upvotes: 1

Rafay
Rafay

Reputation: 31033

$("div").bind("mouseenter",function(){

var divHTML = $(this).html();

});

here is the fiddle http://jsfiddle.net/fveRk/

Upvotes: 1

Sparkup
Sparkup

Reputation: 3754

This sould do it for you :

$('div').mouseover(function(){
   var content = $(this).html();
   var id = $(this).attr('id');
   alert('the element\'s ID is: '+id+'and content is: '+content);
});

Upvotes: 2

citizen conn
citizen conn

Reputation: 15390

$('#yourDivId').hover(function(){
  var value = $(this).html();
});

Upvotes: 2

dmonopoly
dmonopoly

Reputation: 3331

jQuery has a nice way to detect the hovering: http://api.jquery.com/hover/

When you say "get the value" are you talking about getting the div's contents? In that case, the html() jQuery method will do it.

http://api.jquery.com/html/

Upvotes: 0

shelman
shelman

Reputation: 2699

Attach a mouseover event to the div.

Using jquery:

$('#theDiv').mouseover(function() {
    var value = $(this).html();
    //store value
});

Upvotes: 0

Edgar Zag&#243;rski
Edgar Zag&#243;rski

Reputation: 1519

besides the fact that div's doesn't have values, you can store it's text or argument or something else.

var someVar = '';
$(document).ready(function(){
    $('div').mouseover(function(){
        someVar = $('div').html();
        someVar = $('div').attr('id');
        someVar = $('div').attr('class');
        someVar = $('div').find('input').val();
    }
});

Upvotes: 1

ysrb
ysrb

Reputation: 6740

You can try this:

var currentDivID;
var currentDivValue;
$(document).ready(function(){

$('div').mouseover(function(){
  currentDivID = this.id
  currentDivValue = $(this).html();
});

});

Upvotes: 1

Related Questions