Jump to content

Get Text And Put In Element


Macchiato
 Share

Recommended Posts

The following code retrieves the text from the element with id="Text" and puts it in the element with id="putText":

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Get text and put in element</title></head> <body> <p id="Text">$1,200.00</p> <p id="putText"></p> <script type="text/javascript">function getTextAndPutInElement(id) {	var source = document.getElementById(id);	var target = document.getElementById("putText");	target.innerHTML = source.innerHTML;}getTextAndPutInElement("Text");</script> </body></html>

The problem is that $1,200.00 is generated by an other script. When the page is loaded, you will have this result:

<p id="Text">$1,200.00</p><p id="putText">$1,200.00</p>

But in the meantime the text can change:

<p id="Text">$1,500.00</p><p id="putText">$1,200.00</p>

The script with function getTextAndPutInElement only runs ones when the page is loaded, so it doesn't know the text has been changed in the meantime. The content of the element with id="Text" looks something like this: <p id="Text">function otherScript(id) {...code that changes the text}</p> How can you load this script:

function getTextAndPutInElement(id) {	var source = document.getElementById(id);	var target = document.getElementById("putText");	target.innerHTML = source.innerHTML;}

everytime function otherScript is run?

Edited by Macchiato
Link to comment
Share on other sites

if the id here

function otherScript(id){  .};

is the same as ID "Text". why can't you just call getTextAndPutInElement("Text") after you call otherScript()? (or within, as it seems like that would be the flow); i.e.

 function otherScript(id){  //code here  .  .  getTextAndPutInElement(id);};

Edited by thescientist
Link to comment
Share on other sites

if the id here
function otherScript(id){  .};

is the same as ID "Text". why can't you just call getTextAndPutInElement("Text") after you call otherScript()? (or within, as it seems like that would be the flow); i.e.

 function otherScript(id){  //code here  .  .  getTextAndPutInElement(id);};

Thanks for your reply thescientist. I used getTextAndPutInElement(id), but that messes up my page. getTextAndPutInElement("text")however seems to work.
place reference of getTextAndPutInElement("text") in other script after where the new value is passed to the <p id="Text"></p>
getTextAndPutInElement("text") did the trick! I had to play a bit where to put it, but I found the right spot now. Thanks a million :)
Link to comment
Share on other sites

Thanks for your reply thescientist. I used getTextAndPutInElement(id), but that messes up my page. getTextAndPutInElement("text")however seems to work.
right, well I did say in my post that it would work just using getTextAndPutInElement(id) if id was indeed "Text". As you've found out, hardcoding the value is sufficient in this situation though.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...