0

I wanted to create my html component in javascript. Is there any way I can get this div part from javascript function.

My html code

<body>
    <h2>My Customers</h2>
    <input type="text" id="myInput" onkeyup="myFunction()" title="Type in a name">
    <table id="myTable">
      <tr class="header"></tr> 
      <tr><td>Germany</td></tr>
      <tr><td>Sweden</td></tr>
      <tr><td>UK</td></tr>
      <tr><td>Germany</td></tr>
      <tr><td>Canada</td></tr>
      <tr><td>Italy</td></tr>
      <tr><td>UK</td></tr>
      <tr><td>France</td></tr>
    </table>
</body>
5
  • I dont understant your qn??? Commented May 4, 2017 at 9:45
  • Which div part you wanna get? Commented May 4, 2017 at 9:45
  • @MantasČekanauskas I am passing on div by id by using that id I want to create this table by using javascript. Commented May 4, 2017 at 9:46
  • multiple ways, out of which the easiest one, but the not cleanest one is to simply use .innerHTML property and write your entire table in a string that follows. document.getElementById("yourDIV").innerHTML = '<table id="someID">' is an example, just keep piling on. Commented May 4, 2017 at 9:50
  • Or .append() is also a clean one. Had the PO been using jQuery Commented May 4, 2017 at 9:52

5 Answers 5

2

This is my Tic for Tac answer.

var myFunction = function(){

        var div = document.getElementById("yourId");
        var myTable = '<input type="text" id="myInput" + 
          'onkeyup="myFunction()" title="Type in a name">'+
          '<table id="myTable">'+
          '<tr class="header"></tr>' +
          '<tr><td>Germany</td></tr>'+
          '<tr><td>Sweden</td></tr>'+
          '<tr><td>UK</td></tr>'+
          '<tr><td>Germany</td></tr>'+
          '<tr><td>Canada</td></tr>'+
          '<tr><td>Italy</td></tr>'+
          '<tr><td>UK</td></tr>'+
          '<tr><td>France</td></tr>'+
        '</table>';
      div.innerHTML = myTable;

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

4 Comments

i earlier used .append() which was jQuery. Then found jQuery was not used
Yes, but by this way I can add only table. not input.
What input. ? you did not mension any input in the QN. On the comment below that you said you want to add table inside the div. Look at the first comment on your qn. Try to make us understand what do you really need.
yes, there is two tag input and table. I want to add both. Is it possible.
1

For appending you can use insertAdjacentHTML

Working fiddle

function myFunction(){
    var tableStr = `<table id="myTable">
        <tr class="header"></tr> 
        <tr><td>Germany</td></tr>
        <tr><td>Sweden</td></tr>
        <tr><td>UK</td></tr>
        <tr><td>Germany</td></tr>
        <tr><td>Canada</td></tr>
        <tr><td>Italy</td></tr>
        <tr><td>UK</td></tr>
        <tr><td>France</td></tr>
     </table>`;


     document.getElementById('tableContainer').insertAdjacentHTML('beforeend', tableStr)
}

Comments

1

You can use document.createElement() like:

var para = document.createElement("p");
var node = document.createTextNode("This is new.");
para.appendChild(node);

You can refer this link for more details

3 Comments

Ya.. But How I will add table in that
try something like: function(result){ var table = document.createElement("TABLE"); var row = table.insertRow(); for( var key in result ){ var cell1 = row.insertCell(); cell1.innerHTML = result[key]; } }
Thanks for answer. I am ttrying this
0

if you are using jquery,try:

$("#yourdivid").html('Your html code above'); 

with javascript:

 var div1=document.getElementById('yourdivid'); 
 div1.innerHTML='Your html code above'; 

Comments

0

jQuery: this is an example from my code, the var html just makes sure u don't have to write the same piece of code over and over again.

var html = '<div class="swiper-slide">';

html += '<div class="bigIm"><a href="' + url1 + '" target="_blank" class="inner" style="background-image: url(' + img1 + ')">';
html += '<div class="imgCont-b imgCont-b-cov1">';
html += '<a href="' + url1 + '" class="t-icon"><i class="fa fa-' + d1.source.toLowerCase() + ' fa-2x tw-img" aria-hidden="true"></i></a>';
html += '<span class="tw-text">' + d1.source + '</span>';
html += '<p>' + d1.text + '</p>';
html += '</div>';            
html += '</a></div>';  

html += '<div class="bigIm">';
html += '<div class="half first"><a href="' + url2 + '" target="_blank" class="inner" style="background-image: url(' + img2 + ')">';
html += '<div class="imgCont-b imgCont-b-cov2">';
html += '<a href="' + url2 + '" class="t-icon"><i class="fa fa-' + d2.source.toLowerCase() + ' fa-2x tw-img" aria-hidden="true"></i></a>';
html += '<span class="tw-text">' + d2.source + '</span>';
html += '<p>' + d2.text + '</p>';
html += '</div>';            
html += '</a></div>';
html += '<div class="half last"><a href="' + url3 + '" target="_blank" class="inner" style="background-image: url(' + img3 + ')">';
html += '<div class="imgCont-b imgCont-b-cov3">';
html += '<a href="' + url3 + '" class="t-icon"><i class="fa fa-' + d3.source.toLowerCase() + ' fa-2x tw-img" aria-hidden="true"></i></a>';
html += '<span class="tw-text">' + d3.source + '</span>';
html += '<p>' + d3.text + '</p>';
html += '</div>';
html += '</a></div>';
html += '</div>';                                        

html += '</div>';

$('.s1 .swiper-wrapper').append(html);

Hope this helps, keep in mind that this code was meant for a website I made, so ur gonna have to change it a bit.

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.