Pedro Chagas
asked on
Jquery or Javascript - Get the name of the DIV looking for element properties
Hi E's, I have this code that show five boxes. Every one with different positions:
In practice, for example, how I identify in my code the div that have this position: "left: 250px; top: 550px;". The return in this case is "q3".
The solution can be in jQuery (priority) or in Javascript.
The best regards, JC
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Get the element with....</title>
<style>
#q1{position: absolute; width: 100px; height: 100px; left: 50px; top: 150px; background-color: black;}
#q2{position: absolute; width: 100px; height: 100px; left: 150px; top: 150px; background-color: green;}
#q3{position: absolute; width: 100px; height: 100px; left: 250px; top: 550px; background-color: yellow;}
#q4{position: absolute; width: 100px; height: 100px; left: 350px; top: 650px; background-color: red;}
#q5{position: absolute; width: 100px; height: 100px; left: 450px; top: 750px; background-color: blue;}
</style>
</head>
<body>
<div id="q1">a</div>
<div id="q2">b</div>
<div id="q3">c</div>
<div id="q4">d</div>
<div id="q5">e</div>
</body>
</html>
I need to identify the name of the div that have position left: ??? and top: ???.In practice, for example, how I identify in my code the div that have this position: "left: 250px; top: 550px;". The return in this case is "q3".
The solution can be in jQuery (priority) or in Javascript.
The best regards, JC
ASKER
Hi, thanks for the answer. I try your code, but not work.
This is the new code:
~JC
This is the new code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Get the element with....</title>
<style>
#q1{position: absolute; width: 100px; height: 100px; left: 50px; top: 150px; background-color: black;}
#q2{position: absolute; width: 100px; height: 100px; left: 150px; top: 150px; background-color: green;}
#q3{position: absolute; width: 100px; height: 100px; left: 250px; top: 550px; background-color: yellow;}
#q4{position: absolute; width: 100px; height: 100px; left: 350px; top: 650px; background-color: red;}
#q5{position: absolute; width: 100px; height: 100px; left: 450px; top: 750px; background-color: blue;}
</style>
</head>
<body>
<div id="q1">a</div>
<div id="q2">b</div>
<div id="q3">c</div>
<div id="q4">d</div>
<div id="q5">e</div>
<script>
var div_name = $('div[left=250px && top=550px]').id;
alert(div_name);
</script>
</body>
</html>
What is wrong?~JC
try this:
var elements=getElementsByPos({top:150,tag:'div'});
function getElementsByPos(obj)
{
var selector='';
var elements=[];
if(obj.tag)
selector=obj.tag;
if(obj.className)
selector+="."+obj.className;
$(selector).each(function(i,e){
$this=$(this);
var position=$this.position();
if(obj.left&&obj.top)
{
if(position.left==obj.left&&position.top==obj.top)
elements.push($this);
}
else if(obj.left)
{
if(position.left==obj.left)
elements.push($this);
}
else if(obj.top)
{
if(position.top==obj.top)
elements.push($this);
}
});
return elements;
}
wrong question
ASKER
wrong question?
You could always use:
function getElementByPosition(left, top)
{
var element = document.elementFromPoint( left,top);
return element;
}
function getElementByPosition(left,
{
var element = document.elementFromPoint(
return element;
}
ASKER
Hi @ludwigDiehl, I need identify the "div" that have the position left and top, not just top, and in your solution, I think, just give me the "div's" that have position 'top' and not 'left' position. The solution have to give me the div that have in simultaneous 'top' and 'left'.
~JC
~JC
ASKER
Hi again @ludwigDiehl.
I use your last code in this way: (My knowledge in js are very poor)
~JC
I use your last code in this way: (My knowledge in js are very poor)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Get the element with....</title>
<style>
#q1{position: absolute; width: 100px; height: 100px; left: 50px; top: 150px; background-color: black;}
#q2{position: absolute; width: 100px; height: 100px; left: 150px; top: 150px; background-color: green;}
#q3{position: absolute; width: 100px; height: 100px; left: 250px; top: 550px; background-color: yellow;}
#q4{position: absolute; width: 100px; height: 100px; left: 350px; top: 650px; background-color: red;}
#q5{position: absolute; width: 100px; height: 100px; left: 450px; top: 750px; background-color: blue;}
</style>
</head>
<body>
<div id="q1">a</div>
<div id="q2">b</div>
<div id="q3">c</div>
<div id="q4">d</div>
<div id="q5">e</div>
<script>
function getElementByPosition(left,top)
{
var element = document.elementFromPoint(left,top);
return element;
}
getElementByPosition("150","150");
alert(element);
</script>
</body>
</html>
What is wrong in my update?~JC
My first solution actually includes both positions:
This is my example:
I was working on another way:
This is my example:
var elements=getElementsByPos({top:150,tag:'div'});
You could use it this way:var elements=getElementsByPos({left:50,top:150,tag:'div'});
I was working on another way:
var elements=getElementsByPos('div',{left:50,top:150});
function getElementsByPos(selector,position)
{
var type;
if(position.top&&position.left)
type=1;
else if(position.top)
type=2;
else if(position.left)
type=3;
return $(selector).filter(function(){
var pos=$(this).position();
var ok=true;
switch(type)
{
case 1:
ok=pos.top==position.top&&pos.left==position.left;
break;
case 2:
ok=pos.top==position.top;
break;
case 3:
ok=pos.left==position.left;
break;
}
return ok;
});
};
ASKER
Ok, but how I see the var elements, I try put in "alert" but the output is "object Object"?
~JC
~JC
try this:
<div id="results"></div>
<script type="text/javascript">
$results=$('#results');
$.each(elements,function(i,e){
$results.append($(e).attr('id')+'<br/>');
});
</script>
ASKER
Work, thanks.
I do not want to abuse the goodwill, but I need a little help for finish.
I have certain that don't have divs with the same left and top, all elements have different positions, so the result is always 1.
In this case if I want put the result in a normal variable, how I do?
~JC
I do not want to abuse the goodwill, but I need a little help for finish.
I have certain that don't have divs with the same left and top, all elements have different positions, so the result is always 1.
In this case if I want put the result in a normal variable, how I do?
~JC
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi, I don't understand how I put the first result in a normal variable. Please show me.
~JC
~JC
What do you mean by "normal variable"?.
ASKER
Hi. Normal variable is the variable with one value, like var somevar = "a value".
Your solution returns an array, and I want to put a variable the first result of this array, this is the reason for saying normal variable.
After searching I found this solution: "var somevariable = $(e).attr('id');". This solution works. If you were as you were doing?
~JC
Your solution returns an array, and I want to put a variable the first result of this array, this is the reason for saying normal variable.
After searching I found this solution: "var somevariable = $(e).attr('id');". This solution works. If you were as you were doing?
~JC
Yeah of course you can do that. Just remember that in that case it returns a single value as you are passing a dom object as a selector $(element) or in case only one element matches.
However if u use for instance
It may give you no result as there might be several divs on your page
$('selector') will return a single value as it is indeed an array returned thats why you can loop through it using each
However if u use for instance
va somevariable=$('div').attr('id');
It may give you no result as there might be several divs on your page
$('selector') will return a single value as it is indeed an array returned thats why you can loop through it using each
Open in new window
or
Open in new window