SanJeet Singh SanJeet Singh - 5 months ago 8
Javascript Question

Unable to set input value in JavaScript after each reload

This is the code I am using in Chrome's Developer Console:

document.querySelector('input').value = 'Some URL';


The code runs without any error. However, the value of the input is not updated. The input shows the same old value. I can click buttons easily using

document.querySelector('button[value="true"]').click();


So, why are the values not being updated?

UPDATE: I am using this code here. Before commenting that the code works please try it on the page I linked to.

UPDATE 2: I am also unable to debug multiple URLs at once using a for loop. Here is my code:

for(var i=0;i<50;i++){
setInterval(function () {
var url = 'http://myurl/goes/here/?n=' + i;
console.log(url);
document.querySelector('input.inputtext').value = url;
document.querySelector('button[value="true"]').click();
}, 10000);
}


However, it sets the value of input to
http://myurl/goes/here/?n=49
and then reloads the page only once with that value. What I want is reload the page after each URL update so that all the URLs can be scraped at once.

Answer

Based on the code you provided, it looks like you are targeting the wrong element (i.e. the search box at the top as opposed to the main <input /> element) as seen by the result of your code below :

enter image description here

You can resolve this by making your selector more specific within the querySelector() function to target the appropriate element :

document.querySelector('input.inputtext').value = 'Some URL';
Comments