Jump to content
Macchiato

Get Text And Put In Element

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

Share this post


Link to post
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

Share this post


Link to post
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 :)

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...