Reputation: 2413
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
Reputation: 49198
Here is a simple example:
<div id="test">Hello world!</div>
$('#test').mouseover(function(){
var test = $(this);
console.log(test);
});
Upvotes: 1
Reputation: 31033
$("div").bind("mouseenter",function(){
var divHTML = $(this).html();
});
here is the fiddle http://jsfiddle.net/fveRk/
Upvotes: 1
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
Reputation: 15390
$('#yourDivId').hover(function(){
var value = $(this).html();
});
Upvotes: 2
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.
Upvotes: 0
Reputation: 2699
Attach a mouseover event to the div.
Using jquery:
$('#theDiv').mouseover(function() {
var value = $(this).html();
//store value
});
Upvotes: 0
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
Reputation: 6740
You can try this:
var currentDivID;
var currentDivValue;
$(document).ready(function(){
$('div').mouseover(function(){
currentDivID = this.id
currentDivValue = $(this).html();
});
});
Upvotes: 1