Search code examples
javascriptformsmacrosfill

Fill forms in JavaScript


I'm currently a student in Software Engineering, and I'm trying to create a small program (I don't know if the word "macro" is appropriate for it) in HTML or JavaScript, to fill a form from a webpage.

The webpage has the following code, placed in the head section:

input type=password name=code size=8 maxlength=8

input type=password name=nip size=8 maxlength=8

input type=password name=naissance size=8 maxlength=8

I've been thinking of maybe using JQuery, as I've browsed a little bit on the internet to figure out how to do it, but I don't really know how to do that. I'm pretty sure the only way to do it is to modify the values of the fields "code", "nip" and "naissance", but how do I get access to them from an external file?

Please note that I have bases in HTML and JavaScript, but nothing amazing - I'm still learning :/


Solution

  • Since you're looking to use jQuery, this might be a good place to start:

    http://api.jquery.com/attribute-equals-selector/

    Next might be a good tutorial in jQuery to get you started. I'm going to assume you can find those on your own, and I'm going to jumpstart your work:

    var selector = 'input[name="code"]'; // <-- we define the element we want to find here
    // we will use that to select a jQuery element like this -->    $(selector)
    $(selector).val('CODE!!'); // <-- it's just that easy to set a value.
    

    So if that's what it takes to set the value for code from javascript, you can guess what the other two would look like. I'll give you a hint on the second one:

    $('input[name="nip"]').val('NIP!!');
    

    Of course, all this assumes you do use jQuery in the browser to accomplish this