0

I have a website with a Username textbox and a Password textbox and a Sign in button for login. On that website if I login by entering the username and password manually, it works fine. But when I do it using JS like this

document.getElementById('userId').value = 'name';
document.getElementById('pwd').value = 'password';

And click the login button, it shows error "Please enter a username and password for login", i.e. it's not picking up the values.

I also tried despatching the event like this but no luck

var username = document.getElementById('userId');
username.value = 'name';

if ('createEvent' in document) {
  var evt = document.createEvent("HTMLEvents");
  evt.initEvent('change', false, true);
  username.dispatchEvent(evt);
} else {
  username.fireEvent('onchange');
}

var password = document.getElementById('pwd');
password.value = 'password';

if ('createEvent' in document) {
  var evt = document.createEvent("HTMLEvents");
  evt.initEvent('change', false, true);
  password.dispatchEvent(evt);
} else {
  password.fireEvent('onchange');
}

So my only question to experts out here is what should be the Javascript/JQuery code that I can write which will blueprint the logic of changing the textbox value manually.

7
  • Where is HTML code? Commented Jun 24, 2018 at 10:15
  • Do your inputs shows assigned value? Do you validate input values before sending request? Commented Jun 24, 2018 at 10:19
  • @dhaker This is the website URL : irctc.co.in/nget/train-search. Click on "Login" link on top and the login page appears. Commented Jun 24, 2018 at 10:30
  • I think the problem is elsewhere. Can you see the textbox changes when you do it via JS? See this Fiddle example for working code via JS Commented Jun 24, 2018 at 10:40
  • @MahßußMØøn that's what surprising me more. There are no textbox changes neither by doing it via JS nor via typing manually Commented Jun 24, 2018 at 10:48

1 Answer 1

1

After some research, I was able to get the job done by just a small change. Instead of firing onchange event, I had to fire oninput event. I figured this out by Chrome Developer Tool as I saw in Event listeners tab that no "change" event was associated with textbox. Instead, it had "input" event. So the following code fixed it :)

var username = document.getElementById('userId');
username.value = 'name';

if ('createEvent' in document) {
var evt = document.createEvent("HTMLEvents");
evt.initEvent('input', false, true);
username.dispatchEvent(evt);
}
else {
username.fireEvent('oninput');
}

var password = document.getElementById('pwd');
password.value = 'password';

if ('createEvent' in document) {
var evt = document.createEvent("HTMLEvents");
evt.initEvent('input', false, true);
password.dispatchEvent(evt);
}
else {
password.fireEvent('oninput');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.