Finding Out Position Of Html Elements?
Oct 5, 2006Is there a possibility to find out the coordinates, relative to the whole
screen, or at least relative to the browsewindow, where an element ( e.g. link or
picture ) begins or ends?
Is there a possibility to find out the coordinates, relative to the whole
screen, or at least relative to the browsewindow, where an element ( e.g. link or
picture ) begins or ends?
How can I get the position of an element such as a div?
View 2 Replies View RelatedI've been trying the find the (x,y) co-ordinates of an element. I've been using;
document.getElementById(element).style.top
document.getElementById(element).style.left
However this doesn't always work in my script. Now, I've googled how to find the position of an element. And come accross many scripts which supposedly all find the position of any object. Some are very long scripts and some are very short all using a variety of methods. Incidently none of which work, they all return (0,0).
I am trying to dynamically find the position of an anchor on the page. I have a page where you can get to the anchor in the normal way (http://www.mypage.com/index.html#anchorname), but there is a script on that page that adds fields dynamically. This causes IE to lose the place where it was supposed to scroll to (via the anchor).
After the fields are added, I want to rescroll to the location of the anchor. Here is the code that I have running on the page. Code:
I got a script from brothercake which gets the absolute position for an element. Its pretty neat - recursively adding up offsets. I got it from the image transition scripts on his site.
However, this script uses offsetTop, offsetLeft and offsetParent, which dont seem to be supported by FireFox (or maybe I'm doing something wrong).
Here's brothercake's script:
getRealPosition = function()
{
this.pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop;
this.tmp = arguments[0].offsetParent;
while(this.tmp != null)
{
this.pos += (arguments[1] == 'x') ? this.tmp.offsetLeft : this.tmp.offsetTop;
this.tmp = this.tmp.offsetParent;
}
return this.pos;
}
It seems to work for Opera and IE - but not FF. Cant say for other browsers. I've come to rely on it for a part of my 'cross-browser' page. Any thoughts on what I can do?
What I can't figure out is why it seems to work fine on brothercake's site - even in FireFox. I'm supposing that the positioning script must be working because the images appear in the right spot.
I have generated menus from an XML file. The XML also defines sub menus that are to open when the mouse is over any one of the menus.The menus are div tags with text in them. The location of each is based on the order that they added in and the width of the text with some padding.When the users mouse moves over the menu item it should pop up a sub menu at a relative position to the menu item.How do I get that position and width of the menu the user is over if it has never been set explicitly? Is there a way or not? If not what is the best solution for something like this?
View 1 Replies View RelatedI've been trying the find the (x,y) co-ordinates of an element. I've been using;
Code:
document.getElementById(element).style.top
document.getElementById(element).style.left
However this doesn't always work in my script. Now, I've googled how to find the position of an element. And come accross many scripts which supposedly all find the position of any object. Some are very long scripts and some are very short all using a variety of methods. Incidently none of which work, they all return (0,0). I have a question, why would someone create a script to do what one line of code can do? Am I missing something.
I need to come up with a function
function regExpPos (text, re, parenNum) { ... }
that will return the position within text of RegExp.$parenNum if there
is a match, and -1 otherwise.
For example:
var re = /some(thing|or other)?.*(n(est)(?:ed)?.*(parens) )/
var text = "There were some nesting parens in the test";
alert (regExpPos (text, re, 3));
should show 17
Would anyone have one of these?
Is it possible to do this? Say I want to find out where on the screen a specific div is, and i want to know the values of the left and top properties. Can i find this out? If so, how?
View 14 Replies View RelatedI am hoping you can help me. I am finding this problem rather complex to solve. I need to be able to find the surrounding text at the mouse position. For example, if a user clicks on a word in a paragraph, I need to programmatically know what the text is surrounding the click point. The text in question is not just content text, but DOM elements read in as text rather than the DOM element itself. For example, if a user clicks at the word "This" in the following:
<table><tr><td>This is good</td></tr> then I would like to know how to get the text "<td>" just before the "This" as text, not as a DOM element. Furthermore, how can I "enlargen" the scope of my capture, such that I can programmatically get the "<table><tr><td>" part as well as the "is good</td></tr>" part?
Question 1
----------------
I am writing an advanced BBCode system for my forums and I would like
to be able to find where the cursor was positioned last in the text so
I could insert the BBCode there.
Question 2
----------------
Again I am writing an advanced BBCode system for my forums and I would
like to make is so that when someone puts in a [b] tag it goes bold, so
kind of a WYSIWYG editor and also for other things like [img] tags and
[url] tags. So could someone tell me how to do that. I would preferably
like it to still use the textarea tag, or at least a form component so
my existing code works.
I have a number of elements on a page, which I want to remove. The number changes depending on the number of the results,ie. div Id like result1, result2, result3 etc.Is there a way for me to find out the number of items called resultX or is there a way for me to search through all ID's like result ?
View 2 Replies View Relatedhow could I get the element with id "testing123" and append it to the body? or print it to the console? or anything to test that its working.
//JS
$("form[name=create_person]").submit(function(){
console.log("submitted");
[code]....
What would be the easier way to do this? For each of the list elements I want to check how many image elements are inside each, and do something with the one that has only one image.
<ul>
<li>
<img src="" />
[code]....
I'm trying to use unobtrusive javascript for a page I am building. I want to insert a "bookmark me" item into the page. This is the script I am using:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL]">
<html xmlns="[URL]" xml:lang="en" lang="en">
<head><title>Page</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" type="text/css" href="/css/base.css" />
<script type="text/javascript">
<!--
function add_favorite() {
document.getElementById('bookmark').innerHTML="<a href="#" rel="sidebar" onclick="if(navigator.appName=='Microsoft Internet Explorer'){ window.external.AddFavorite(location.href, document.title); return false; }else{ this.title = document.title; }" onMouseover="window.status='Bookmark Us';return true" onMouseout="window.status=";return true" title="Bookmark This Page"><img src="/images/bookmark65-3.png" alt="Bookmark This Page" /></a>";
} window.onload=add_favorite();
-->
</script></head><body>
<div id="wrapper">
<div id="header">
<h1>Heading goes here...</h1>
<div id="bookmark">
</div></div>
<div id="main">
<h2>Content goes here...</h2>
</div><div id="footer">
Footer goes here...
</div></div></body></html>
It's pretty simple stuff that just populates an empty div with an image link to bookmark the page when users have javascript enabled. Now when I call the function using <body onload="add_favorite();"> and cancel out the window.onload=add_favorite(); from the script it works just fine (it throws a line-1 syntax error up in IE that I can't figure out, but functionally everything still works in IE7 and FF2). When I try to do it the "unobtrusive" way I get nothing at all in either browser.
A little debugging revealed that the function is indeed being called and is running successfully, however the function is somehow unable to locate the element with ID of "bookmark" when it is fired with window.onload and as a result I get no bookmarking link. It's almost like window.onload is triggering itself too soon, before the div of "bookmark" is in the page, but how can that be?
I'm trying to create a piece of code that will check a webpage and replace any instances of a specific line of HTML with another line of HTML. The code I have so far doesn't work, but I have a feeling that's down to my awful Javascript skills! For example, in the following code I'd want to replace all instances of
<li><a href="index.pdf">Index</a></li> with <li><a href="contents.pdf">Contents</a></li>
<div id="downloads">
<p>Please click on your downloads below:</p>
<ul>
<li><a href="index.pdf">Index</a></li>
<li><a href="chapter1.pdf">Chapter 1</a></li>
<li><a href="chapter2.pdf">Chapter 2</a></li>
<li><a href="chapter3.pdf">Chapter 3</a></li>
<li><a href="credits.pdf">Credits</a></li>
</ul>
</div>
The Javascript I have so far is:
<script type="text/javascript">
var aEls = document.getElementsByTagName('a');
for (var i = 0, aEl; aEl = aEls[i]; i++) {
aEl.href = aEl.href.replace('index.pdf','contents.pdf');
}
Obviously this is meant to just replaces the a href elements, but I got a bit stuck after this!
Is it possible to move text elements over page and save their position?My page could be printed without background image (paper in printer looks like background image). I want to move text elements over page so text elements, after printing, could fit any document similar to background image.
View 4 Replies View RelatedIs it possible to move text elements over page and save their position?Picture in attachment shows one part of my page. This page could be printed without background image (paper in printer looks like background image). I want to move text elements over page so text elements, after printing, could fit any document similar to background image.
View 4 Replies View RelatedI have HTML tags stored in XML. I want to be able to use these HTML elements with Javascript, just as you can with elements in document.body. How can it be done? (And don't try and tell me I should use server-side because I have written it all for Javascript and the project is nearly complete minus this and there are practical reasons for not doing this server-side. After all, anything is possible with Javascript!)
Let me explain:
- I have HTML templates such as this [URL]
- I want javascript to populate these templates then add them to my page
- The only way I know javascript can get this kind of data is by parsing XML
- I want to parse the XML then be able to use the HTML elements just like those in document.body
- As far as I'm aware, XML is the only good way of storing data for javascript. I don't want to store it in javascript variables (too much multiline data with " and '). Nor do I want to build it using document.createElement("div")... etc
As someone not yet with any experience in computer science etc, please ignore my poor terminology! However, I'm not a beginner when it comes to javascript.
Here's the script concerned but I doubt it'll help you understand my problem: [URL]
I'm using the following code to get tooltips to appear for elements within my SVG DOM:
function tooltip_event_for_svg_node(svgDoc, nodeID) {
var node_obj = svgDoc.getElementById(nodeID);
$(node_obj).tooltip({
delay: 0,
track: true,
[Code]...
The SVG object is defined as follows:<object id="svg_obj" data="svg_graph.svg" type="image/svg+xml" /> and there are <div> objects which correspond appropriately to each of the href attribute values on the elements within the SVG source (svg_graph.svg). The tooltips show up as expected--except their coordinates are extremely offset up and to the left from the pointer, instead of sitting right and below the pointer as expected. I discovered that when I removed HTML from the page source, this distance shrunk. When I removed everything except the <object> (and the appropriate js) from the HTML, the tooltip was in the right location. When I used tooltip in other (non-SVG) areas of the page, I didn't have this issue.
I'm busy trying to build an interface where users can create a sort of collage with images that they upload. I know it is possible to get the position of a DHTML element relative to the screen, the problem is I need the coordinates relative to a main div tag so that I can "compile" the collage and then when it gets served it will look the same regardless of screen size.
View 2 Replies View RelatedI'm trying to create a button that floats down the side of the screen. Position:fixed;ostly does what I want it to do, but I'd like the element to only track with scrolling in a certain area (so that it stops below the header, and above the footer). I'm sure this is possible with JS, but I can't seem to figure out how to make it work
View 1 Replies View RelatedI'm trying to grab the X/Y co-ordinates of given elements on a page, and scroll to them using the window.scrollTo() method. This is working for standard text boxes (INPUT objects), but for drop down lists (SELECT objects), the JQuery .position() method isn't returning the result object:
var el = $("#[id$=" + elements[i][1]);
if(el.position() && el.position().top) {
var top = el.position().top;
var left = el.position().left;
window.scrollTo(top, left);
break;
}
This works just fine for text boxes, but will not work for selects/drop down boxes. I've stepped through the code and the element el is always populated correctly, so its not the get statements that is at fault. If I inspect the value of el.position().top I get 'null or not an object' and el.position() returns 'undefined'.
I'm learning jQuery with XML. I'm familiar with other query languages such as XPath. I'm having a little bit of difficulty wrapping my brain around how jQuery works but I think I can make the leap if I see a solution to a problem I know how to solve with other methods Given the following XML, please share a jQuery solution to finding the value of attribute B in elements named bar where attribute A has the value 30:
[Code]...
I was editing some javascript code yesterday that happened to use jquery. Part of the code selected two elements via jquery. Then the original author used nodeIndex to determine the position of each of these nodes in their parents. I could not find any documentation on nodeIndex on the jQuery site or anywhere else on the internet. It seemed to work in IE8, but I did not see the property from FireBug in FireFox 3.5. So I removed the call to nodeIndex and used a for loop to calculate my own equivalent of the nodeIndex. Is nodeIndex officially supported or meant for internal use? Is it documented anywhere? [URL]
View 1 Replies View RelatedI had askedthis question on another forum but unfortunately I was unable to find a fix. I have a select and drag code that works well until in selection is added a position relative div. Here is the code and you can see a working demo at [URL] To see the problem just mouse select span 1, span 2 and the nested span 4 You can see the code and demo at http://jsbin.com/azeli/2
[Code]....