user3296544
user3296544

Reputation: 25

document.getElementById for chaining IDs

I have a html:

<div id="sliderFrame"><div id="slider"></div></div>

I want to insert html code into slider div, and I know

$('#slider').append('<a class="lazyImage" href="sample.jpg"></a>');

will work.

I am wondering if I can achieve this using innerHTML, but I failed to do it because document.getElementById().getElementById() can not work. And I found chaining getElementById , but I don't quite get it, any one has simpler solution or can explain this?

Upvotes: 0

Views: 756

Answers (3)

a2345sooted
a2345sooted

Reputation: 599

you could do

var slider = document.getElementById("slider");
slider.innerHTML = '<a class="lazyImage" href="sample.jpg"></a>';

but like the comments said, why would you want to do this? What you have is fine.

Upvotes: 0

schnill
schnill

Reputation: 955

is this what you want ?

document.getElementById('slider').innerHTML = '<a class="lazyImage" href="sample.jpg">  
                                               </a>'

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174624

There can only be one id in a page (see the spec):

7.5.2 Element identifiers: the id and class attributes

Attribute definitions

id = name [CS]

This attribute assigns a name to an element. This name must be unique in a document.

So assuming your document is well-formed, you only need one getElementById().

Upvotes: 0

Related Questions