0

Sorry if this is a stupid question, I am doing a project in JQuery and I do not have time to physically learn it so I am effectively jumping straight in.

So I am editing a piece of code which is originally this:

$('#login').click(login);

So when the login button is clicked the login function is called. Adding the line:

$('#login').click();

After that line works as expected, triggering the click and then runs the login function. But simply replacing those two lines with:

login();

HTML

<form action="#">
    <div class="form-group">
        <input type="text" id="nickname" class="form-control" placeholder="Enter nickname" autofocus> 
   </div> 
   <button type="submit" id="login" class="btn btn-primary btn-block">Connect</button>   
</form>

Javascript

var elem = document.getElementById("nickname"); 
elem.value = "tom"; 
$('#login').click(login); 
$('#login').click();

Does not work at all? I don't get it. Surely I am just calling the function and bypassing having to have the actual button in DOM?

Update I do have the function login defined below where the click function is in the code itself, I just felt it unecessary to post that code here as it is only calling the function that is the problem.

Update 2 Here is the login function that is being called:

function login(event) {

    event.preventDefault();

    params = {
        login: $('#nickname').val(),
        password: '123123123' // default password
    };

    // check if user did not leave the empty field
    if (trim(params.login)) {
        $('#loginForm form').hide();
        $('#loginForm .progress').show();

        // chat user creation
        QB.users.create(params, function(err, result) {
            if (err) {
                onConnectFailed();
                alertErrors(err);
            } else {
                chatUser = {
                    id: result.id,
                    login: params.login,
                    pass: params.password
                };

                // chat user authentication
                QB.login(params, function(err, result) {
                    if (err) {
                        onConnectFailed();
                        alertErrors(err);
                    } else {
                        connectChat();
                    }
                });
            }
        });
    }
}
kabeersvohra
  • 971
  • 1
  • 10
  • 27
  • 2
    Where/how do you call `login()`? Where is it defined? Show more code to help you finding the issue... – Alex Gidan Dec 09 '14 at 10:19
  • So the form is in the html file `
    ` and I am trying to simulate the entering of the form myself with `var elem = document.getElementById("nickname"); elem.value = "tom"; $('#login').click(login); $('#login').click();`
    – kabeersvohra Dec 09 '14 at 10:23
  • Sorry I have no idea how to format that better in the comments... – kabeersvohra Dec 09 '14 at 10:24
  • You should put these snapshots in the question itself (you can edit it)... – Alex Gidan Dec 09 '14 at 10:26
  • @KVohra95 You're probably using `this`or `event`object inside the function. Which we can't tell for sure unless you show the function. – T J Dec 09 '14 at 11:06
  • ok I will add the function to the question – kabeersvohra Dec 09 '14 at 11:08

4 Answers4

1

it depends on the scope where you call login function.

Check where you have something like

function login(){
   //some code
}

and be sure that the region of code where you are calling the function can see it.

Have a read here

update

looking at your code I can see that you haven't a login function but you are just submitting the form because login input is of type "submit".

try just

$("#idOfTheForm").submit();

assuming that

<form action="#" id="idOfTheForm">

update 2

I think that some information are missing about your code. As you are saying it's impossible that something happens if you have no handlers. If you don't have a login function neither an action for your form it's impossible that something happens. Maybe you have some other code in others files.

update 3

I think that the only problem in your login function is that it has 1 parameter input and as first line of code you use it (the error breaks the execution).

try in this way

function login(event) {

 if (event != null)   
     event.preventDefault();

//other code

}

and then call it simply with login();

faby
  • 6,838
  • 3
  • 24
  • 40
  • Hi. Yes I do have a login function below that gets called. The objective of this is to avoid having to include an unecessary form in the code to the submit method is slightly defeating the point. Thanks for your help though – kabeersvohra Dec 09 '14 at 10:29
  • Did you add the id to your form as I wrote in the answer? – faby Dec 09 '14 at 10:41
  • maybe could be the action of the form because it is empty `action="#"`. Did you remove something from there? – faby Dec 09 '14 at 10:44
  • That was the same action that has worked before. The action works on the click jquery method as normal – kabeersvohra Dec 09 '14 at 10:51
  • I've updated my answer. There is something missing in your code – faby Dec 09 '14 at 11:04
  • I have added the login function to the question, could you please have a look if you get a chance? thanks – kabeersvohra Dec 09 '14 at 11:13
  • This doesn't work either :/ Thanks for all your help I guess I will just use the original approach even though it is far from ideal – kabeersvohra Dec 09 '14 at 11:26
  • check if the function login is called and let em know – faby Dec 09 '14 at 11:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66468/discussion-between-faby-and-kvohra95). – faby Dec 09 '14 at 11:27
  • try removing that line of code and then remove event parameter – faby Dec 09 '14 at 11:28
0

$('#login').click() is like a trigger; when user chick on #login, then it launch the code inside ()

$( "#login" ).click(function() {
  alert( "Handler for .click() called. on login" );
});

if you just do

function login(){}

there will be a function that do the work you want but the function will never be called.

if you do

function login(){}
login();

it signify that you have a function that do the work you want and the function is launched when code is running, and not when user click on something.

clement
  • 4,260
  • 10
  • 58
  • 129
  • what is "that"? what do you want exactly? – clement Dec 09 '14 at 11:21
  • Sorry, I did exactly what was in the question but it was not working in the original question. I am trying to debug the reason for it to not be working here but no one seems to know why – kabeersvohra Dec 09 '14 at 11:28
0

When you do

$('#login').click(login);

you're telling you want the login() function to be called as handler of the event "click" on "login" element.

In the code above I don't see where you defined your login() function. If you do a login() and "nothing happens" it's because the function is not defined from the place you're calling it...

You should have somewhere in your HTML code a snapshot looking like this:

<script>
    function login(...){
     //....body....
    }
</script>
Alex Gidan
  • 2,332
  • 14
  • 26
  • Not to mention that the $().click() will pass an event as argument to the function. – Kroltan Dec 09 '14 at 10:28
  • The login function is defined in the same file as the js that calls it. @Kroltan it does have an event as a parameter! I didnt realise that the event was generated from the click. Is there any way to create the event without the click action? Thanks – kabeersvohra Dec 09 '14 at 10:33
  • Yes, call the click action once and print the event argument. Then when you call it without the jQuery click, pass it an argument similar (with the same attributes) to that function. – Kroltan Dec 09 '14 at 10:52
  • @Kroltan thanks, the event was [object Object] which didn't seem to solve my problem – kabeersvohra Dec 09 '14 at 11:12
  • Don't alert it, do a `console.log`, then in the console click it. it will show the whole object. Alternatively, print its JSON representation, using `JSON.stringify` – Kroltan Dec 09 '14 at 11:31
0

In the previous two cases you have id of the element "login" where your function login() is written.but in the last case you just have a function in your code which is not called by you. so, it was not working.

for first two cases i think your  code is some thing like 
<div id="login">login</div>
<script>
 $('#login').click(function(){
  //some code
 });
</script>

you can find the similar question here(jQuery.click() vs onClick). for the third one the login() is not triggered any were

Community
  • 1
  • 1
sitaram9292
  • 171
  • 8