How to swap contents of a div using javascript?

1

I'm trying to create a script to replace everything inside a div with something predefined, I'm new to javascript and I'm having trouble finding a solution for this, I've looked in various places, but I have not achieved anything.

I am calling the script using url.searchParams.get

if(url.searchParams.get("sub") == 1) {
    document.getElementById("rep").innerHTML = "<p>Outro texto</p>"
}
<div id="rep">Texto</div>
    
asked by anonymous 20.07.2018 / 17:34

1 answer

0

The searchParams is a property of the object URL . Therefore, to use it you must first create the object with a valid URL. If you want to use the current URL of the page in the object, just use the location.href property:

let url = new URL(location.href);

With the object created in the url variable, you can access the url.searchParams property using get() :

let url = new URL(location.href);
if(url.searchParams.get("sub") == 1) {
    document.getElementById("rep").innerHTML = "<p>Outro texto</p>"
}

If the page URL contains a sub=1 value, if will be accessed and the div HTML will be replaced.

    
20.07.2018 / 17:54