Not My Name Car
Not My Name Car

Reputation: 59

Incorrect Javascript Functions?

I am completely brand new to Javascript. In fact, I'm just an Objective-C programmer looking to implement just a little Javascript into one of my apps. So, the problem is for whatever reason when I call my first function, nothing happens there I don't get the alert. When I call my second function I only get the alert, the button isn't actually clicked.

Code:

function myFunction(username, password) {
    alert("Data will be entered");
    document.getElementById('ctl00_plnMain_txtLogin').value = username;
    document.getElementById('ctl00_plnMain_txtPassword').value = password;
}

function click() {
    alert("The button was clicked");
    document.getElementById("ctl00_plnMain_Submit1").click();
}

I can seem to run just a regular alert fine, but nothing else??? Is there something wrong in my function?

If it helps, on the website here is the "username" box:

<input name="ctl00$plnMain$txtLogin" type="text" maxlength="50" id="ctl00_plnMain_txtLogin" tabindex="1">

"password" box:

<input name="ctl00$plnMain$txtPassword" type="password" maxlength="255" id="ctl00_plnMain_txtPassword" tabindex="2">

the button:

<input onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(''); " name="ctl00$plnMain$Submit1" type="submit" id="ctl00_plnMain_Submit1" tabindex="3" value="Log In" title="Log in">

-- EDIT

Also, I am starting the functions through my objective C code. I grab a js file, then I am able to use its functions on a webpage.

Upvotes: 0

Views: 83

Answers (2)

SoWhat
SoWhat

Reputation: 5622

I am guessing you want the click function to be called when the button is clicked

You need to specify that then

  <input onclick="click()" ....

Upvotes: 0

Slukehart
Slukehart

Reputation: 1037

I would recommend using a DOM abstraction library like jQuery

jQuery provides an 'on' method, it's used to bind events to DOM elements:

$('#ctl00_plnMain_Submit1').on('click', function (event) {
    alert("The button was clicked");
}

It also provides a nice abstraction for collecting input values:

var userName = $('#ctl00_plnMain_txtLogin').val();

You can also use it to set the value of an input:

$('#ctl00_plnMain_txtLogin').val('New Value for This Input');

Populating inputs with a function:

function populateInputs (username, password) {
    $('#ctl00_plnMain_txtLogin').val(username);
    $('#ctl00_plnMain_txtPassword').val(password);
}

Upvotes: 1

Related Questions