3

What I'm trying to do is fill out a form that uses Materialize but what it's happening is the written content just overlapping the input's placeholder and not being actually "received" from the page. The JS code I'm using is the following:

$("label[for='firstname']").addClass("active"); 
document.getElementById("firstname").value = "John";

Because on the web I've noticed that many users fixed this problem addind the class "active" to the input's label, but this is not working for me.

Also, when trying to use

  $(document).ready(function() {
Materialize.updateTextFields();
  });

I get an error on the webpage that says "Uncaught ReferenceError: Materialize is not defined".

Is there another way to fix this issue? I'm autofilling using a Chrome extension and I cannot modify the html page content by injecting scripts with document.write.

Edit:

I've also noticed that if write manually in the input field, the class of the input goes from

materialize-element-field ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required ng-valid-pattern ng-valid-minlength ng-valid-maxlength

to

materialize-element-field ng-valid-pattern ng-valid-minlength ng-valid-maxlength active ng-touched ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required

The class label is not changing at all.

There's also the following div before the input field:

<div ng-class="['materialize-element',
                            {
                                'input-half left': true,                                
                                'materialize-textinput': true,
                                'materialize-password': false,
                                'materialize-textarea': false,
                                'materialize-select': false,
                                'materialize-number': false,
                                'materialize-checkbox': false,
                                'materialize-radio': false,
                                'materialize-block-radio': false,
                                'collapsed': !selectBoxExpanded['firstname'] &amp;&amp; false,
                                'expanded': selectBoxExpanded['first'] &amp;&amp; false,
                                'has-icon': false &amp;&amp; true,
                                'required': requiredField['ship.firstname'] || true,
                                'not-empty': !isModelEmpty(ship.firstname || form['ffirstname_46afgt3'].$$rawModelValue),
                                'valid': (isModelModified(form, 'firstname_46afgt3') &amp;&amp; form['firstname_46afgt3'].$valid) &amp;&amp; form['firstname_46afgt3'].$viewValue.length != 0,
                                'error': (isModelModified(form, 'firstname_46afgt3') &amp;&amp; form['firstname_46afgt3'].$invalid),
                                'disabled': false,
                                'has-counter': false
                            }]" ng-click="selectBoxExpanded['firstname']=!selectBoxExpanded['firstname']" ng-init="initField('ship', 'firstname', '', false)" class="materialize-element input-half left materialize-textinput required">

The class of the div above changes to the following after I manually add text in the input:

materialize-element input-half left materialize-textinput required not-empty valid

Edit 2:

The input field is the following before the text:

<input class="materialize-element-field ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required ng-valid-pattern ng-valid-minlength ng-valid-maxlength" type="text" id="firstname" name="firstname_46afgt3" value="" maxlength="33" ng-pattern="/^[\u00C0-\u1FFF\u2C00-\uD7FF\u2019a-zA-Z\.\'\-\s]{1,}$/" ng-required="requiredField['ship.firstname'] || true" ng-model="ship.firstname" ng-init="validateField('firstname_46afgt3', true, 'W3sidHlwZSI6InJlcXVpcmVkIiwibGFiZWwiOiJmb3Jtcy5jaGVja291dC5kZWxpdmVyeS5tZXRhcGFjay5hZGRyZXNzLmZpcnN0bmFtZS5taXNzaW5nIiwiZGlzcGxheUxhYmVsIjoiSW5zZXJpc2NpIGlsIG5vbWUuIn0seyJ0eXBlIjoicGF0dGVybiIsImxhYmVsIjoiZm9ybXMuY2hlY2tvdXQuZGVsaXZlcnkubWV0YXBhY2suYWRkcmVzcy5maXJzdG5hbWUuaW52YWxpZCIsImRpc3BsYXlMYWJlbCI6IkNvbnRyb2xsYSBpbCBub21lLiJ9XQ==')" ng-keydown="ngKeyHandler($event)" ng-change="ngChangeHandler('firstname', 'firstname_46afgt3', false)" ng-trim="true" required="required">

And then this is after I write text in it:

<input class="materialize-element-field ng-valid-pattern ng-valid-minlength ng-valid-maxlength active ng-touched ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required" type="text" id="firstname" name="firstname_46afgt3" value="" maxlength="33" ng-pattern="/^[\u00C0-\u1FFF\u2C00-\uD7FF\u2019a-zA-Z\.\'\-\s]{1,}$/" ng-required="requiredField['ship.firstname'] || true" ng-model="ship.firstname" ng-init="validateField('firstname_46afgt3', true, 'W3sidHlwZSI6InJlcXVpcmVkIiwibGFiZWwiOiJmb3Jtcy5jaGVja291dC5kZWxpdmVyeS5tZXRhcGFjay5hZGRyZXNzLmZpcnN0bmFtZS5taXNzaW5nIiwiZGlzcGxheUxhYmVsIjoiSW5zZXJpc2NpIGlsIG5vbWUuIn0seyJ0eXBlIjoicGF0dGVybiIsImxhYmVsIjoiZm9ybXMuY2hlY2tvdXQuZGVsaXZlcnkubWV0YXBhY2suYWRkcmVzcy5maXJzdG5hbWUuaW52YWxpZCIsImRpc3BsYXlMYWJlbCI6IkNvbnRyb2xsYSBpbCBub21lLiJ9XQ==')" ng-keydown="ngKeyHandler($event)" ng-change="ngChangeHandler('firstname', 'firstname_46afgt3', false)" ng-trim="true" required="required">

It seems that the "value" tag in the input field is not being touched at all and if I try to manually edit it from Chrome Dev console, the same thing as autofill happens.

Daniel
  • 3,541
  • 3
  • 33
  • 46
L. K.
  • 139
  • 2
  • 12
  • Can you add an example of the input field not recieving the value? On http://materializecss.com/forms.html, I get the following: document.getElementById('first_name').value = 'test' => "test" document.getElementById('first_name').value => "test" – Eric N Oct 30 '16 at 14:15
  • Added more documentation to the op, hope that helps – L. K. Oct 30 '16 at 16:13
  • Likely to be solved with http://stackoverflow.com/q/22843563/934239 – Xan Oct 31 '16 at 15:23
  • It's quite clear from the question that the form in question is using Angular. You need to trigger an event that would signal to Angular instance in the page context that the value changed (as assigning `.value` of an element is not triggering that). The dupe I used to close proposes a solution in its top answer, using jQuery in the content script context (which you also use). – Xan Oct 31 '16 at 15:26
  • Using: "$('#firstname').val("John"); $('#firstname').trigger('input');" isn't working neither. – L. K. Oct 31 '16 at 15:59
  • Try with `click`, `keydown`, `keyup`.. But if all that doesn't work I'll remove the dupe. – Xan Oct 31 '16 at 19:21
  • I've tryed to trigger all the events I could think and nothing worked. So yes, I tryed with the events you suggestes too. – L. K. Nov 01 '16 at 10:33
  • Just figured it out! All I needed to do is to inject the code into the web page itself and then it worked using "$('#firstname').val("John"); $('#firstname').trigger('input');". Seems that if it is just into a Content Script, Angular give troubles. – L. K. Nov 01 '16 at 11:01

1 Answers1

-1

Ah, if I use the last_name example, I can see how the input looks like its overwriting the placeholder. It isn't actually a placeholder though. Its the label being positioned within the input area. When the input receives focus, it moves the label above. Without receiving focus (or the active class manually) the label remains in the same space that the input fields value resides in.

For your JS error, that is saying that the Materialize script isn't loaded. If you open the console on the page you're at and type in Materialize, does it return undefined or an object? If undefined, Materialize's JS isn't being applied to that page.

If it returns an object, I'd check if its possible that your script is executing before the Materialize script is executed.

Edit:

I just noticed that Materialize applies the "active" class to the label via javascript instead of just css states. That would indicate that the Materialize script must be on the page. Can you provide a link of the page you're working on?

Community
  • 1
  • 1
Eric N
  • 2,136
  • 13
  • 13
  • When I type "Materialize" in the console it still gives me the ReferenceError. Unfortunately is not possible for me to provide you a link. – L. K. Oct 30 '16 at 15:53
  • Added more documentation and page source in the op to help you analyze the problem better. Thanks for you help! – L. K. Oct 30 '16 at 16:09
  • The "ng-*" classes indicate they are using angular, and I believe an angular-wrapped version of materialize: http://krescruz.github.io/angular-materialize/#about . That unfortunately will change a lot and make what your'e doing a bit more difficult. I don't think you'll be able to hook into the Angular ecosystem. Let me spin up a demo codepen and see if any sort of hook is possible to the Materialize object inside of the Angular object. – Eric N Oct 30 '16 at 16:21
  • Looking into Angular-materialize, it appears it still loads Materialize into the window. So it should be accessible to you. However, I think you need to execute your script differently since its a chrome extension! I'm not very familiar with extensions but I think they run on their own process and don't share the window's variables. So then, based on this post, it looks like you need to inject a script into the DOM which can then execute from the window like you are trying to do in the example: http://stackoverflow.com/questions/20499994/access-window-variable-from-content-script Good luck! – Eric N Oct 30 '16 at 16:42
  • So you think I should inject that way in the webpage a js and then run in this js the code I need to autofill correctly? – L. K. Oct 30 '16 at 22:09
  • Just tryed, it does the exact same thing because I'm actually using a content script to autofill that's the same thing as having a js inside the page itself. Any way to actually fix this with JavaScript code? – L. K. Oct 30 '16 at 22:26
  • 1
    Did you do it correctly? Example: [Building a Chrome Extension - Inject code in a page using a Content script](//stackoverflow.com/q/9515704) – wOxxOm Oct 31 '16 at 00:40
  • The code is running correctly, so yes. – L. K. Oct 31 '16 at 07:22
  • Added the input field in the op, maybe it will be helpful! @wOxxOm – L. K. Oct 31 '16 at 12:24