-1

getTime = () =>{
    time = document.getElementById("time").innerHTML
    console.log(time)
 }

Hi how to get this time value out to a different script? just the syntax would be greatly helpful. i am new

edit : = i am working on a plugin i want to use the value of time in a different js file. to better manage. prepared a js just to scrap three text(time being one of them) from the opened site. then i want to do rest of the thing in a a different js file. if any one can share with example. would immensely appreciate. Thank you

Weee
  • 1
  • 2
  • by moving it to a different script you mean you want to use that value outside your getTime function? – Chris G Jun 02 '22 at 14:10
  • 2
    Not sure what you're asking here but if instead of logging it, you do `return time;` you can now do `const t = getTime();` elsewhere in your script to get the current innerHTML – ChrisG Jun 02 '22 at 14:10
  • hey i want to use the value of the time in a different js file i am working on a chrome extension i want to use the value of time in a different js file. to better manage. prepared a js just to scrap three text(time being one of them) from the opened site. then i want to do rest of the thing in a a different js file. if any one can share with example. would immensely appreciate. – Weee Jun 02 '22 at 14:20
  • Don't think in terms of "files", think in terms of "scope". You just need the `time` variable to be available in the scope chain of the other code that you want to be able to use it. This can be done by declaring `time` in a scope that is available to both bits of code or have the `getTime()` function `return time`, making it available to the caller. – Scott Marcus Jun 02 '22 at 14:26
  • oh ok. ty just to clarify say the code is same as above and i do return time (in the function) and store the value in t(using const t=getTime();) could u please share the syntax on how to use the value of t in another js cus when i am trying (console.log(t) - in another js file ) -_- it gives error t not declared -_- i have done this too as internet claimed it will unify both their scope(if not? if u could clarify. ty) – Weee Jun 02 '22 at 14:36
  • 1
    As I said, it's not about the files, it's about the scope of the variable and the scope chain that is in effect when you need to access `t`. Please read [this other post](https://stackoverflow.com/questions/41385835/invocation-context-and-execution-context-in-javascript-are-we-talking-of-th/41386097#41386097) to better understand scope and the scope chain. – Scott Marcus Jun 02 '22 at 14:46
  • ok ok i understand where the issue is(i hope) i need to globalize the scope of t to be accesed everywhere. one final thing. if i am doing getelementbyid. can u please share a syntax so that if i am looking for "time" id to copy its value it only looks in one link of a site which i define.(not in other other html page i have saved in the same directory as the js file). sry i am 1 day new – Weee Jun 02 '22 at 15:01

0 Answers0