0

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.

5
  • wat does this do if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(''); Commented Mar 23, 2013 at 6:26
  • The problem is nobody is calling those function, where do you want the functions to be called Commented Mar 23, 2013 at 6:27
  • Sorry for not explaining that part-I'll update. Commented Mar 23, 2013 at 6:30
  • I feel like you need a bit more help here, but I'm struggling to understand what your trying to accomplish - it looks like your setting the value of those inputs to window.username / window.password, I don't think that is what you intend correct? Commented Mar 23, 2013 at 6:41
  • On a webpage there are two text boxes, one for a username and one for a password, I was trying to send arguments with my function above to set the text in these boxes to the arguments :D -- Am I setting it in the wrong place? Should I be somewhere else other than document? Commented Mar 23, 2013 at 7:04

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

8 Comments

Bare with me here, I don't know a single thing about Java ! Could I use this method to change the values of text boxes?
I've updated my answer to include an example of setting the value of an input.
Can I shove all of that into a function, using variables for the inputs?
Yup let me update, my answer - where is the value for the variables coming from? How will you call the function, just on page load?
@Not My Name Car your function should work just as well without jquery. 2 small corrections, Java is NOT Javascript, and "Bare with me" means to get naked together...ewww.
|
0

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()" ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.