Link to home
Start Free TrialLog in
Avatar of montypy
montypy

asked on

How can I pass a string to a document.forms?

Hello

How can I pass a string to a document.forms[1][0] and submit using Chrome DevTools?
Avatar of montypy
montypy

ASKER

So for example , I want to pass a user_name to this field:
<input type="text" name="log" id="user_login" class="input" ">

Open in new window

Avatar of Michel Plungjan
You mean

(function() {var LF = document.getElementById("user_name"); LF.value="Bart"; LF.form.submit()})()

or

(function() {var F = document.forms[1]; LF = F["log"]; LF.value="Bart"; F.submit()})()
In DevTools goto the Console tab and there you can enter and run any js commands you want.
Avatar of montypy

ASKER

For this, I get :
(function() {var LF = document.getElementById("user_name"); LF.value="Bart"; LF.form.submit()})()

Open in new window

TypeError: Cannot set property 'value' of null
Probably there is no element with ID user_name but


This makes no sense - LF refers to element, I assume a text box, so it doesn't have a FORM
LF.form.submit
Avatar of montypy

ASKER

You were right, the element is "user_login", So now the username is submitted correctly. However, I get a new error- "TypeError: object is not a function"

. I need to submit the username and password and submit. The password element ID is "user_pass"


(function() {var LF = document.getElementById("user_login"); LF.value="Bart"; LF.form.submit()})()
TypeError: object is not a function

Open in new window

Can you post the FORM html.
Avatar of montypy

ASKER

<form name="loginform" id="loginform" method="post" _lpchecked="1">
		
		<p>
    <label>Username:<br>
		<input type="text" name="log" id="user_login" class="input" value="" size="20" tabindex="10" autocomplete="off" style="cursor: auto; background-image: url(data:image/png;base64,base64c=); background-attachment: scroll; background-position: 100% 50%; background-repeat: no-repeat;"></label>
		</p>
		
		<p>
    <label>Password:<br>
		<input type="password" name="pwd" id="user_pass" class="input" value="" size="20" tabindex="20" autocomplete="off" style="cursor: auto; background-image: url(data:image/png;base64,bas64c=); background-attachment: scroll; background-position: 100% 50%; background-repeat: no-repeat;"></label>
		</p>
				
		<p class="submit">
		<input type="submit" id="submit" value="Login »" tabindex="100">
		</p>
<script type="text/javascript">
<!-- 
		document.loginform.submit.disabled= false;
// -->
</script>
<noscript>
		&lt;p&gt;&lt;b&gt;ERROR: Javascript is required for this page; please enable it in your browser.&lt;/b&gt;&lt;/p&gt;
</noscript>
		
	</form>

Open in new window

Michel already gave you the correct code above (just slightly amended to target by name)

(function() {var F = document.forms['loginform']; LF = F["log"]; LF.value="Bart"; F.submit()})() 

Open in new window

Avatar of montypy

ASKER

When I enter that into the Console it does not log into the website. Also, the function does not provide a use_pass element.
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of montypy

ASKER

Thanks! that did it
Avatar of montypy

ASKER

Thanks again for staying with me :) I really appreciate it!
If that worked then mine should have worked too had I been allowed to see the form. document.forms[1] is the SECOND form on the page, if you wanted the first one, it was document.forms[0] - I just used your information.

(function() {
  var F = document.forms['loginform'], LF = F["log"], LP=F["pwd"]; // all are local vars
  LF.value="Bart"; 
  LP.value="password"; 
  F.submit()
})();

Open in new window

Avatar of montypy

ASKER

It does not work:

(function() {
  var F = document.forms['loginform'], LF = F["log"], LP=F["pwd"]; // all are local vars
  LF.value="Bart"; 
  LP.value="password"; 
  F.submit()
})();
TypeError: F.submit is not a function

Open in new window

That is because there is an object in the form that is named "submit"

<input type="submit" id="submit" value="Login »" tabindex="100">
so either that needs to be renamed or you have to click the submit button just like suggested by Gary