10

I have created an <input> HTML element using Javascript. Now I want to add an onblur event handler to this element dynamically. However I do not understand how I can pass the created element as an argument to the function. Here's my code:

element = document.createElement("input");
element.onblur = hello_function;

In the above code you can see that the element is created. Now I want to pass that element to hello_function. How can I do that?

function hello_function(element) {
    alert(element);
}
2
  • 1
    hello_function.bind(element). Also, I'd suggest you to use .addEventListener instead of .onblur Commented Nov 15, 2016 at 8:08
  • You don't need t pass anything? You already can access that element inside the hello_function via this or event.currentTarget. Commented Nov 15, 2016 at 14:01

3 Answers 3

9

To achieve this you can wrap the hello_function call in an anonymous function wrapper and provide the this argument:

element = document.createElement("input");
element.addEventListener('blur', function() {
  hello_function(this);
});
document.body.appendChild(element);

function hello_function(element) {
  console.log(element);
}

Also note the preferred use of addEventListener over onblur.

Sign up to request clarification or add additional context in comments.

7 Comments

Out of curiosity, if I want to access the event object as well inside hello_function(), how would that be passed.
@asprin add a additionnal event parameter to the hello_function() and to the addEventListener. Demo jsfiddle.net/xp5we163/82
@aspirin see this question for details
I'm sorry you feel that way. Thankfully this website allows you to add your own answer. Also, I'm looking forward to seeing your use of an arrow function which works reliably in IE, whose support is still a common requirement of most commercial and enterprise level websites.
@StephanBijzitter That makes no sense. Of course arrow functions work differently (that's like claiming "bind doesn't work because someone could change it to a call"). Binding it explicitly instead of just dynamically using it will only be less performant, but has no advantages.
|
1

try like this. passing the another variable into a function,

var something="hello";
var element = document.createElement("input");
  element.addEventListener('blur' , function ()
                           {
    hello_function(something);
  
  })
 document.body.appendChild(element)

 function hello_function (element){
      alert(element);
}

Comments

1

i suggest to use addEventListener, also i think you need to append the created element to the document, something like this:

var elem = document.createElement("input");
if (elem) {
  elem.addEventListener('blur', hello_function, false);
}
document.body.append(elem);
function hello_function(element) {
  alert(element);
}

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.