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();
}
});
}
});
}
}