I am trying to get one div
from one webpage URL to another webpage and display in plain text using getElementById
without using AJAX or jQuery because I'm going to implement it in FitNesse. Is there a way to pass the URL?
If you need to get the IDs from the HTML on another HTML page, you have to literally load that page. You can do this programmatically using JavaScript (look up AJAX), but you have to load the HTML page, it doesn't exist somewhere floating around in the ether just waiting for you to grab things from it.
A commonly-used alternative to document. getElementById is using a jQuery selector which you read about more here.
The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.
HTML DOM Document getElementById()The getElementById() method returns an element with a specified value. The getElementById() method returns null if the element does not exist. The getElementById() method is one of the most common methods in the HTML DOM.
You could load the URL in a hidden iframe.
Then use iframe.contentWindow.document.getElementById($id) as outlined here: How to pick element inside iframe using document.getElementById
Something along the lines of:
<iframe src="urlWithinYourDomain.html" style="display:none"></iframe>
Followed by a function something like:
var divElement = document.getElementById('iframeId').contentWindow.document.getElementById('elementIdOnSourcePage');
document.getElementById('targetParentId').appendChild(divElement);
I'm afraid I can't test this at the moment, so there may be syntax errors, but I think it conveys the idea. It's a bit of a dirty approach, but given your constraints it's the best way I can see to do what you're asking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With