0

Desired output in HTML:

<input onclick="someFunction(this)"/>

Attempt:

var input = $("<input/>");
input.attr("onclick", "someFunction(this)");
$("#body").append(input)

Result:

<input onclick="[object Object]"/>

I have tried using:

input.click({param: "this"}, someFunction);

I have also tried replacing "this" with "input". No luck.

jQuery is not a requirement, however I need this done dynamically. The above is just a sample, the actual object has more attributes.

3
  • Read docs: api.jquery.com/click Commented Sep 29, 2020 at 19:30
  • I attempted examples from here and derivations from there, however I am still unable to figure it out. I keep getting the same result as above. Commented Sep 29, 2020 at 19:36
  • You can forward the event target to the function call using a lambda expression: input.on('click', e => someFunction(e.target)) Why does it matter whether you render an attribute or create the listener in memory? Commented Sep 29, 2020 at 19:36

3 Answers 3

2

All is working from the box...

Here is example. Just click on input

var input = $("<input/>");
input.attr("onclick", "someFunction(this)");
$("#body").append(input);

function someFunction(obj) {
  $('#result').css('color', 'red');
  console.log(obj);
}

$('#result').text(input[0].outerHTML);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<body id="body">
  <div>This is your code:</div>
  <div id="result"></div>
</body>

And you can also use pure jQuery. The $(this) is what you need.

var input = $("<input id=\"xxx\"/>");
input.click(function() {
  const theInput = $(this);
  console.log(theInput[0]);
});
$("#body").append(input);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body id="body"></body>

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

Comments

1

You could create the listener in-memory instead of adding the handler as an attribute. This version lets you wrap your event target in a jQuery object ahead of the function call.

const someFunction = ($element) => {
  console.log($element.prop('class'));
}

const $input = $('<input>', { class: 'foo' })
  .on('click', (e) => someFunction($(e.target)))
  .appendTo('body');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

But if you really need to add it as an attribute, you could do the following.

const someFunction = (element) => {
  console.log($(element).prop('class'));
}

const $input = $('<input>', { class: 'foo' })
  .attr('onclick', 'someFunction(this)')
  .appendTo('body');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Comments

1

Use arrow function, this will be inherited.

input.on('click', () => someFunction(this))

3 Comments

Be aware that the lambda does not redefine this so you are good, but if you used input.on('click', function() { someFunction(this) }), you would have a scoping problem.
Ofc, yes. That's the glory of arrow functions.
In your example this will be not an input, maybe you wanted: input.on('click', () => someFunction(input))?

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.