Jump to content

Hey guys,

 

So I am trying to do the following: assign a "default" value to a javascript variable, then change that value, and to then again be able to change the variable,
 

<script>
function functie1(x) {
var z = "jasonr";
var x = prompt("Which chat do you want to see?", "Shroud");
if (x == "") {
   window.alert("You canceled.");
   location.reload();
} else {
   window.alert("You chose " + x );                                                               
   document.getElementById( z ).src = "https://www.twitch.tv/" + x + "/chat?popout=";
   document.getElementById( z ).id = x;  
}}
document.getElementById("clickMe").onclick = functie1;
function functie2(y) {
var k = "lirik";
var y = prompt("Which chat do you want to see?", "lirik");
if (y == "") {
   window.alert("You canceled.");
   location.reload();
} else {
   window.alert("You chose " + y)
   document.getElementById( k ).src = "https://www.twitch.tv/" + y + "/chat?popout=";
   document.getElementById( k ).id = y;
}}
document.getElementById("clickMe2").onclick = functie2;
var y = k;
var x = z;
</script>

this is my script, I am altering Iframe elements with it, what I want to achieveis that I can alter the elements multiple times where I can only do that once at this moment.

I think it has something to do with that there is no element left that goes byid = z after it has been altered once, so now I thought I had to change the value of z into x after the function ran, but I cant get it to work.

Can someone tell me what's what with the variable scopes or how I can assign the new values to it, I've looked all over but I am not getting wiser at all.

 

Thanks in advance!

 

Bob

Link to comment
https://linustechtips.com/topic/893475-javascript-variables/
Share on other sites

Link to post
Share on other sites

looks like k and z are block-scoped, so they're inaccessible outside the function. try returning k and z then change k and z at the global scope to the function call

 

when i run your script on the devtools, and removing the onclicks (to hide the error), i get k is undefined

 

check out Kyle simpson's YDKJS for more on closures and scope

Link to comment
https://linustechtips.com/topic/893475-javascript-variables/#findComment-11013678
Share on other sites

Link to post
Share on other sites

26 minutes ago, Technicolors said:

looks like k and z are block-scoped, so they're inaccessible outside the function. try returning k and z then change k and z at the global scope to the function call

 

when i run your script on the devtools, and removing the onclicks (to hide the error), i get k is undefined

 

check out Kyle simpson's YDKJS for more on closures and scope

Thank you! I will check them out.

Link to comment
https://linustechtips.com/topic/893475-javascript-variables/#findComment-11013783
Share on other sites

Link to post
Share on other sites

4 hours ago, Bob van Daal said:

Thank you! I will check them out.

You should also rewrite that code into a single function then pass the things that are different as it stands you basically have the same code twice. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/893475-javascript-variables/#findComment-11014636
Share on other sites

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

×