Ajay
Ajay

Reputation: 21

How do I change a parameter in a URL upon submitting in JavaScript

I want the following URL to be manipulated and opened in a new tab once I pass the value of email ID and click submit. The searchText in myUrl is the param for email ID:

HTML:

<textarea name="email" id="email" cols="30" rows="10"></textarea>
<button id="myButton">submit</button>

JavaScript:

const myUrl = new URL('[email protected]&site=something&mobileVersion=&locale=&forceAaaApplication=');

Upvotes: 2

Views: 95

Answers (1)

you can use this:

HTML:

<button id="myButton" onclick="sbButtonClick()">submit</button>

JavaScript:

const textarea = document.getElementById("email");
let myUrl = null;

function sbButtonClick() {
    myUrl = new URL(`something.com?searchText=${textarea.value}&site=something&mobileVersion=&locale=&forceAaaApplication=`);

    // do something with myUrl
}

Edit

to get multiple email addresses one of the easiest ways is this way:

const textarea = document.getElementById("email");

function sbButtonClick() {
    // split the text by line breaks so each address should be in a separate line
    const emailArray = textarea.value.split("\n");

    // here you open tab for each email address
    for (let address of emailArray) {
        window.open(`something.com?searchText=${address}&site=something&mobileVersion=&locale=&forceAaaApplication=`);
    }
}

Upvotes: 2

Related Questions