Accessing elements in DIV

X

Xerxes

Hi,
I have a <div> in my page with some elements. How can I access these
elements inside the DIV? I tried document.getElementById("element-id")
but it is not right.

If the elements are inside a form, how would I access them?

TIA.
 
M

Martin Honnen

Xerxes said:
I have a <div> in my page with some elements. How can I access these
elements inside the DIV? I tried document.getElementById("element-id")
but it is not right.

document.getElementById should do if the element you are looking for has
an id attribute. It searches in the complete HTML document however, not
only inside of the div. If you want to search relative to the div
element then you can use the getElementsByTagName method of the div and
of course the properties like firstChild, lastChild and the childNodes
collection.

If the elements are inside a form, how would I access them?

A form element object has a property named elements in which you will
find form controls like input, textarea, select and button elements so
you can use
document.forms.formName.elements.controlName
 
X

Xerxes

document.getElementById should do if the element you are looking for has
an id attribute. It searches in the complete HTML document however, not
only inside of the div. If you want to search relative to the div
element then you can use the getElementsByTagName method of the div and
of course the properties like firstChild, lastChild and the childNodes
collection.


A form element object has a property named elements in which you will
find form controls like input, textarea, select and button elements so
you can use
document.forms.formName.elements.controlName

Thanks Martin.
I am using this in the context of an asp.net page.
<div style="border:1px solid #000; width:100%; height: 150px;
overflow:auto;" id="divSelectedRecord">
<span style="float:left;width:150px">Vendor</span>
<span id="spVendor" style="float:left;margin-left:90px"></span>
<span style="float:left;width:150px">Sequence Number</span>
<span id="spSeqNum" style="float:left;margin-left:20px"></span>
<br />
</div>

Then in the script:

var v = document.getElementById('<%=spVendor.ClientID%>'); <--- it
complains spVendor does not exist in the current context

v.innerHTML = some_value;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top