1

I have created a loop and below is the part of the code between that loop

--- Loop Starts ---

var aElement = $('<a>');
aElement.attr('href', '#');
aElement.text(title);

aElement.click(function() {
    alert("Hello World);
});

video_list_html += '<tr>' +
                        '<th scope="row">' + count + '</th>' +
                        '<td>' + aElement + '</td>' +
                    '</tr>';
--- Loop Starts ---

But as aElement is an object, it doesn't attach as html tag to the video_list_html but it attaches like this

[object Object]

How do I fix this, so that it attaches inside the video_list_html and also the .click() event stay working.

1
  • 1
    You cannot concat aElement as string without loosing event listeners. If you don't want to use the event listener, you have to append aElement using appendChild Commented Jul 16, 2017 at 14:29

2 Answers 2

1

Try using aElement[0].outerHTML instead of aElement. Leave click handler as is.

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

4 Comments

Who should this keep the event listener? aElement.outerHTML would result in a string with out any informations about event listeners that have been attached.
True, it appends the html tag fine but the click event doesn't work. Just tried to confirm
The new element will need another handler if that's what he wants, of course, the first handler for the original element remains untouched. I understood that's the idea
The old element and the new element created using HTML string are two different elements, naturally that another element needs another handler, I meant to say that first handler will still work for the first element.
0

The issue is because you cannot append an object to a string. The object gets coerced, ans the result is, as you've seen, [Object object].

A better solution to this problem would be to append the new HTML as one entire string, then use a delegated event handler to catch the events from those dynamically added elements. Try this:

var data = [{
  videoId: 'abc123',
  title: 'Video #1'
}, {
  videoId: 'xyz987',
  title: 'Video #2'
}]

var video_list_html = data.map(function(o, i) {
  return '<tr><th scope="row">' + i + '</th><td><a href="#" data-videoid="' + o.videoId + '">' + o.title + '</a></td></tr>';
}).join('');

$('#yourTable').append(video_list_html).on('click', 'a', function(e) {
  e.preventDefault();
  console.log($(this).data('videoid'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="yourTable"></table>

2 Comments

The thing is where I have done alert(), I have actually called my own function with and ID as argument that I got in the loop. Like this displayVideoAnalytics(videoId); to be straight, I am working on youtube analytics api and this was one of the code from it which I modified for my needs
Don't do that. Put the videoId in a data-* attribute on the element which you can retrieve within the single delegated click event handler. I updated the question to show you how to do this.

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.