1

I'm trying to accomplish the following: when a div is clicked the user will go to a new url and it adds a query string to the url.

For example the user is on this url:

www.example.com/step1/?query1=abc&query2=def

When a div is clicked the user should go to:

www.example.com/step2/?query1=abc&query2=def&query3=ghi

Sarvesh Mahajan
  • 914
  • 7
  • 16

3 Answers3

0

Let suppose you have a div (ie. <div id="mydiv" />), you can add a click eventListener which will be executed when you click the div, you can do it like:

let mydiv = document.getElementById('mydiv')

mydiv.addEventListener('click', () => {
  window.location.href = 'http://example.com/step2/?query1=abc&query2=def&query3=ghi'
})

I used vanilla Javascript, but if you are using jQuery it will be much easier

0

May be this can help you. You can change the url with desired one

<!DOCTYPE html>
<html>
<body>

<script ype="application/javascript">
function myFunction() {
location.replace("https://stackoverflow.com/");
}
</script>

<div onclick="myFunction()">
<h2>test</h2>
</div>
</body>

lote
  • 50
  • 8
0

Try this:

function go(){
var currentUrl = window.location.href;
var targetUrl = 'http://example.com/?query3=value';
var newUrl = currentUrl + "&" + targetUrl.split("?")[1];
console.log(newUrl);
//window.location.href = newUrl;
}
<div onclick="go()">GO</div>
Ritesh Khandekar
  • 3,885
  • 3
  • 15
  • 30