1

I am using self.webView.stringByEvaluatingJavaScript(from: "document.getElementById('username').value = \"\(username)\"") to put in a username, and it works fine.

A different webpage has this field:

<input type="text" maxlength="255" name="user" class="stdWidth" value="">

and this field:

<input type="password" maxlength="255" password="1" name="pass" class="stdWidth">

How would i use stringByEvaluatingJavaScript to put text into this field, as it has no ID?

I hace tried "document.getElementByTagName" but it didnt work.

The webpage I am trying to input to is: Link

ZiEiTiA
  • 275
  • 2
  • 16

1 Answers1

3

Try like this.you have class field so use document.querySelector() to select your element.Example below.

var input = document.querySelector(".stdWidth");
input.value = "\"Hello\"";
//add event listener on edit like this..
input.addEventListener('change',function(){
var value = this.value;
console.log(value);//this is current value of your field
});
  
<!-- begin snippet: js hide: false console: true babel: false -->

UPDATE

The getElementsByTagName() method returns a collection of all elements in the document with the specified tag name, as a NodeList object.

var input = document.getElementsByTagName('input');
input[0].value = "\"username\"";
input[1].value = "\"password\"";
<input type="text" maxlength="255" name="user" class="stdWidth" value="">
<input type="text" maxlength="255" name="pass" class="stdWidth" value="">
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
  • `self.webView.stringByEvaluatingJavaScript(from: "document.querySelector('.stdWidth').value = \"\(username)\"")` did the job - thanks! – ZiEiTiA Feb 18 '17 at 11:10
  • Yes use `document.querySelector()` because you have `class` field.And then set value as you set like in `id`. – Hikmat Sijapati Feb 18 '17 at 11:11
  • `` I also want to edit this box, however it has not value - can you also tell me how to edit this? – ZiEiTiA Feb 18 '17 at 11:13
  • yes it is possible to edit you have to handle `change` event on your `textbox`. – Hikmat Sijapati Feb 18 '17 at 11:14
  • how do i do that? Can you give me some example code? – ZiEiTiA Feb 18 '17 at 11:15
  • You can see in edited answer.Just added event listener to get your field value on editing. – Hikmat Sijapati Feb 18 '17 at 11:19
  • `self.webView.stringByEvaluatingJavaScript(from: "document.getElementByTagName('pass').value = \"\(password)\"")` How would I update this code to change the pass field above? Would your code edit the password field or return when the username field is changed? – ZiEiTiA Feb 18 '17 at 11:23
  • if you want to set field blank initially then just remove `input.value = "\"Hello\"";`. – Hikmat Sijapati Feb 18 '17 at 11:23
  • same way as you did for `username`. – Hikmat Sijapati Feb 18 '17 at 11:24
  • if you need to save fields values on editing in database then need to use `ajax()`. – Hikmat Sijapati Feb 18 '17 at 11:25
  • `self.webView.stringByEvaluatingJavaScript(from: "document.querySelector('.stdWidth').value = \"\(username)\"") self.webView.stringByEvaluatingJavaScript(from: "document.querySelector('.stdWidth').value = \"\(password)\"")` This just causes the first field to contain the password, and the password field remains empty – ZiEiTiA Feb 18 '17 at 11:28
  • see this line for pasword.`"document.querySelector('.stdWidth').value = \"\(password)\"")` you have to use new class name for field password. – Hikmat Sijapati Feb 18 '17 at 11:31
  • how do I do that? – ZiEiTiA Feb 18 '17 at 11:31
  • `'` then `"document.querySelector('.pass').value = \"\(password)\"")` try like this ` – Hikmat Sijapati Feb 18 '17 at 11:32
  • I can't edit the webpage i'm inputting to - I can't change the class of the password field. This is the webpage I am putting the fields into [link](https://usagym.org/fw/login.html?url=/pages/index.html) – ZiEiTiA Feb 18 '17 at 11:33
  • try once like this.. `self.webView.stringByEvaluatingJavaScript(from: "document.getElementByTagName('input')[1].value = \"\(password)\"")` – Hikmat Sijapati Feb 18 '17 at 11:35
  • putting that in causes the username field to not work, and doesn't put anything into the password field. – ZiEiTiA Feb 18 '17 at 11:40
  • see edited answer use offset to get your element.Because `document.getElementByTagName()` returns array of elements of tag name. – Hikmat Sijapati Feb 18 '17 at 11:45
  • The issue is that the pass field doesn't have a 'value' attribute. – ZiEiTiA Feb 18 '17 at 11:48
  • ok.. I still haven't got anything to work though - the username field is setting fine but I can't set the pass field. – ZiEiTiA Feb 18 '17 at 11:54