1

I am creating a form from javascript. I have to put the onclick on the a tag to call a method when clicked. I am doing like this, but it is not appearing the onclick.

function remPass() {
        var log = document.getElementById("login");
        var form = document.createElement('form');
        form.method = "post";
        form.action = "";
        log.appendChild(form);
        var input = document.createElement('input');
        input.type = "text";
        input.name = "username";
        input.className = "username";
        input.placeholder = "Usuario";
        var a = document.createElement('a');
        a.className = "bt-enter pink";
        a.id = "recordar";
        a.href = "#";
        a.onclick = "remember()"; //Here is the problem
        a.innerHTML = "Recordar contraseña";
        form.appendChild(input);
        form.appendChild(a);
    }
5
  • if remember() is a function, why is it a string? remove the " " around remember() Commented May 8, 2015 at 8:44
  • how about adding the "HTML code" instead of creating the form element from scratch? Commented May 8, 2015 at 8:45
  • @NachoDawg, and the parentheses. Commented May 8, 2015 at 8:45
  • @Asier GR, if you want to attached onclick event method, setAttribute would be sufficient. Commented May 8, 2015 at 8:54
  • That was the error, I didn't realise. Thanks Commented May 8, 2015 at 8:56

5 Answers 5

5

You need use method, not method's result nor a string

a.onclick = remember;

And don't forget to create

function remember(){
  //some code
}
Sign up to request clarification or add additional context in comments.

2 Comments

And what if you want to call a function with some parameter?
if you write remember(param1, param2), when it's called by click, param1 would be event object and second would be undefined. also you can call this function by yourself, passing any parameters you like to
4

the correct way of doing is like that

  a.onclick = remember; //do like that

  var remember = function(){
           //remember logic..
  };

Comments

2

the right way is

  a.onclick = remember; //without double quotes

  var remember = function(){
           //more code here...
  };

Comments

1

You should use setAttribute method to set attributes / properties for the tag while creating it:

var a = document.createElement('a');
    a.setAttribute("class", "bt-enter pink");
    a.setAttribute("id", "recordar");
    a.setAttribute("href", "#");
    a.setAttribute("onclick ", "remember();");
    a.innerHTML = "Recordar contraseña";

This is the most competent method of creating elements cross-browser.

Comments

0

You should give something of type function not a string:

a.onclick = remember;
//....
var remember = function() {
// remember logic
}

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.