I want JavaScript to click a button when a parameter is in the url (&startCalc=1).
html:
<input name="startCalc" value="Start" type="submit">
javascript:
var parameterExists = url.searchParams.get("startCalc");
if(typeof parameterExists !== 'undefined' && parameterExists == 1) {
document.getElementsByName('startCalc').click();
}
I get an error: "click is not a function". I want to simulate a click the same way as an user would would do it when clicking on that button...
Two things you need to change First searching parameter in URL and second when getting button by name you get an array so you need to specify the index of that. Check the code below: DEMO
let url = "https://jsfiddle.net&startCalc=1"; // Suppose you are getting url in here
let urlParams = new URLSearchParams(url);
let parameterExists = urlParams.get('startCalc');
if(typeof parameterExists !== 'undefined' && parameterExists == 1) {
document.getElementsByName('startCalc')[0].click();
console.log('button clicked!')
}