No errors but I'm stuck on a strange problem with my JS code and I don't know what I'm doing wrong

not allowed to use links yet so i spaced them out to bypass, no hate please i just need help with this issue im having

Basically, I am limited to one singular line of JavaScript code which I made be this:

javascript:var s = document.createElement('script');s.type='text/javascript';document.body.appendChild(s);s.src='https :// hastebin . com /raw/ediveqixey';void(0);

what this does is creates a variable with the refence of s (short for script) equal to a script element created in the document. It sets the script type to text/javascript and then it adds the script to the bottom of the body, and sets the source equal to a hastebin link in raw form which is js code, and then void(0) is just evaluating to undefined

this allows me to run a multi-line snippet of code I made instead of being limited to just one line

WITH THAT BACKGROUND EXPLAINED

let’s move onto the issue now:

https :// hastebin. com /raw/ediveqixey

this link is some js code i made:


var hidden = 0;

var Markup = prompt("Insert HTML here");

var cMarkup = ""

let div = document.createElement('div');
div.className = "alert alert-success";

document.addEventListener("keypress", function onPress(event) {
	if (event.key === "=") {
		if (hidden == 0) {
			hidden = 1;
			cMarkup = document.documentElement.innerHTML
			div.innerHTML = Markup;
		} else {
			hidden = 0;
			div.innerHTML = cMarkup;
		};
	} else if (event.key === "-") {
		Markup = prompt("Insert HTML here");
	};
});

to break this up, it creates four variables. hidden which is just a bool value, Markup which is set to user-input string from a prompt, and cMarkup which is an empty string

div variable is a div element and it sets the classname of the div to “alert alert-success”

then it creates an eventlistener for a user’s keypress

it checks if the user presses the “=” key on their keyboard and if they do press the equal key then it checks if the hidden value is false or not. if it’s false it sets it to true and then it copies the cMarkup (current Markup) of the page’s innerHTML before setting the div’s innerHTML to the Markup which was obtained through the user’s input. If hidden is true and not false then it sets hidden to false and sets the div’s innerHTML to the cMarkup of the page before it got swapped to the Markup the user set with the prompt. It also checks if the key pressed was the “-” key and if it was then it prompts the user to enter the HTML markup in case they want to change what that variable is.

The code works perfectly, except that when the div’s innerHTML is changed nothing happens to the page. It doesn’t erase the page’s HTML markup and replace it with the one the user provides from the prompt, and when it switches back to being unhidden it doesn’t change the page when it switches the innerHTML back to the cMarkup. I can’t figure out why this doesn’t change the page because I have another one-line snippet of code which is this:

javascript:var markup = prompt(“enter html of page here”); let div = document.createElement(‘div’); div.className = “alert alert-success”; div.innerHTML = markup;

what this does is it creates a variable defined as “markup” which is set to the string of the user’s input from a generated prompt and then it creates a variable defined as “div” which is set to a created div element and then it sets the div’s className to “alert alert-success” and sets the div’s innerHTML to the markup which was given by the user, and when this is run it works perfectly and switches the page content, so I’m trying to figure out why it works if you manually do it but why my attempt at automating it so I dont have to run it a bunch of times doesn’t work

thank you for reading this and sorry that it’s so lengthy. if you have any questions or need clarification or are confused then please ask me and I will answer and try to clear up confusion

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums