JavaScript Jquery Appended Button Not Displaying Correctly jquery append button to div,jquery append button with onclick,

I am working with Jquery/javascript/html. I am trying to display a button inside of tags in my table. I am appending the information into/onto a section on my html page. Code is as follows:

<html>
<body>
    <p id="report_area"></p>
</body>
</html>

Javascript file below

$('#report_area').append('<table>'); 
$('#report_area').append('<tr>'); 
$('#report_area').append('<th>' + view + '</th><th>' + col_1 + '</th><th>' + col_2 + '</th><th>' + col_3 + '</th>');

$('#report_area').append('</tr>');
var btn=$('<button/>');
btn.text('View');
btn.val=item.SURVEY_JOB_ID;
btn.id=item.SURVEY_JOB_ID;

// recently added code - start
btn.click(function()
    {
      window.localStorage.setItem("MyFirstItem", 10);
      window.location = 'GoToThisOtherPage.htm'
    }
// recently added code - end
$('#report_area').append('<tr><td>'+ btn +'</td><td>' + item.JOB_NUMBER + 
'</td><td>' + item.TITLE + '</td><td>' + item.MODIFICATION_NUMBER + '</td></tr>');
$('#report_area').append('</table>');

THis seems to work correctly however, the button is not showing up correctly. It shows up as an object. All the other data displays correctlyMy table row is displayed as :

[object Object] 12 New Job Title 0

[object Object} 30 Title Help Me 1

I'm not sure why it is displaying as [object Object]. When I do something as simple as:

$('#report_area').append(btn);

the button shows up on the page correctly. Any help on this would be greatly appreciated. Thanks in advance.

Answer:1

To understand why this does not work, you have to look at the documentation for append.

Type: htmlString or Element or Array or jQuery

append is able to except any of those types, and handle each of them differently, so when you pass it an element (actually jQuery collection), it is able to intelligently convert it into the desired html.

However, in your case, you are passing it a string, so it will naively treat the string as html. The reason that this produces [object Object], is because it is relying on native JavaScript to convert the element into a string. You'll produce the same output with console.log(btn).

// append recieves a jQuery collection, calls appropriate methods to obtain html
$('#report_area').append(btn);
// append receives a string, blindly assumes that it is already the desired html
$('#report_area').append(btn + '');

Solution 1 - Append separately

From your comments on other answers, it doesn't seem like this solution works. I think this is because append will automatically add the close tags for the tr and td when appending, causing the button to be added afterwards. You could check if this was the case by looking at the html produced in the developer tools of your browser.

$('#report_area').append('<tr><td>', [btn, '</tr></td>'])

Solution 2 - Convert to string properly

$('#report_area').append('<tr><td>'+ btn[0].outerHTML +'</td><td>')

Solution 3 - Constructing everything as jQuery collections

I think the main problem you have been having is to do with mixing elements and strings. I've written a working jsfiddle solution that constructs everything as jQuery collections.

var table = $('<table>');

var btnRow = $('<tr>');

var btnCell = $('<td>');

var btn=$('<button>');
btn.text('View');
btn.val('val');
btn.attr('id', 'id');

btn.on('click', function()
    {
      window.alert('Click');
    });

btnCell.append(btn);
btnRow.append(btnCell);

table.append(btnRow); 
btnRow.append('<td>1</td><td>2</td><td>3</td>');

$('#report_area').append(table); 
Answer:2

JavaScript is converting btn to a string because you're concatenating several strings to it.

It should work if you do this.

$('#report_area').append('<tr><td>');
$('#report_area').append(btn);
$('#report_area').append('</td><td>' + item.JOB_NUMBER + 
'</td><td>' + item.TITLE + '</td><td>' + item.MODIFICATION_NUMBER + '</td></tr>');
$('#report_area').append('</table>');
Answer:3

On the moment I am building code to show text when I hover on a certain div. I am able to produce this, but it's static and I can't reproduce it. I would like to make it dynamic. I have a single ...

On the moment I am building code to show text when I hover on a certain div. I am able to produce this, but it's static and I can't reproduce it. I would like to make it dynamic. I have a single ...

  1. show/hide div jquery
  2. show/hide div javascript
  3. show/hide div css
  4. show/hide div on link click
  5. show/hide div on page load javascript
  6. show hide divs
  7. show hide divs jquery
  8. show hide divs javascript
  9. show hide divs on click
  10. show hide div with transition css
  11. show hide div on scroll position
  12. show hide div using jquery
  13. show hide div using javascript
  14. show hide div in angular 6
  15. show hide div javascript onclick
  16. show hide div with animation javascript
  17. show hide div on button click
  18. show hide div codepen

Mainly I am trying to figure out how to update alert.currentPage for each route. I switched everything to a directive but still am at a loss. Any direction would be helpful. Thanks! REWRITTEN ALL ...

Mainly I am trying to figure out how to update alert.currentPage for each route. I switched everything to a directive but still am at a loss. Any direction would be helpful. Thanks! REWRITTEN ALL ...

How can you add a class to a tag with a custom attribute? Most people use ID, but in my case I need to use a custom attribute. CSS: .correct { background-color: lightGREEN; } JQuery: $(".save")...

How can you add a class to a tag with a custom attribute? Most people use ID, but in my case I need to use a custom attribute. CSS: .correct { background-color: lightGREEN; } JQuery: $(".save")...

  1. classu003d custom-file-input
  2. classu003d custom-select
  3. classu003d custom-control-input
  4. class custom details and tires
  5. classu003d custom-file
  6. classu003d custom-file-input bootstrap
  7. class custom carriers
  8. classu003d custom-file-label
  9. class custom
  10. class custom css
  11. class custom attribute c#
  12. class custom pickle
  13. class custom decorator python
  14. class custom ring
  15. class custom cout
  16. d&d custom class
  17. kotlin data class with custom setter

I'm trying to have same size images with flexbox. I give a display: flex; to my slick-track container and then flex: 1 0 auto to my img. I thought that with a flex-grow of 1 my image will increase if ...

I'm trying to have same size images with flexbox. I give a display: flex; to my slick-track container and then flex: 1 0 auto to my img. I thought that with a flex-grow of 1 my image will increase if ...

  1. same height images css
  2. same height images bootstrap
  3. same height images
  4. equal height images css
  5. equal height images bootstrap 4
  6. equal height images flexbox
  7. equal height images
  8. same height couples images
  9. flex same height images
  10. same height and width images
  11. slick slider same height images
  12. owl carousel same height images
  13. images same height html