Teresita Galagar
Teresita Galagar

Reputation: 3

Get all tags inside a div using javascript

Newbie in javascript.

Suppose I have this html

    <html>
    <div id = "div1">
      <table>
       <tr><td>Name</td></tr>
       <tr><td>Password</td></tr>
      </table>
    </div>
    <div id = "div2">
      <input name="place" value=""/>
      <input name="place" value=""/>
    </div>
    </html>

But I want to retrieve all the tags and elements of a div in "div1". So elements that will be retrieved will be

          <table>
           <tr><td>Name</td></tr>
           <tr><td>Password</td></tr>
          </table>

Not only the data.

Upvotes: 0

Views: 3460

Answers (2)

Shouvik
Shouvik

Reputation: 11740

First and foremost, congratulation on getting started on JS. It's a beautiful scripting language. Next, I would advise you, to make your life simpler while you should play around with a pure Javascript syntax, it will make your life a whole lot easier if you get started on a library, like jQuery or Mootools. JQuery is my preference, mootools is more of a framework.

document.getElementById('div1').innerHTML;

in jquery is as elemental as $(#div1) or $(#div1).html(). Have fun learning!

Upvotes: 1

user2582303
user2582303

Reputation: 56

if you use this in your code you will be able to get the table which your saying

document.getElementById('div1').innerHTML;

Upvotes: 2

Related Questions