Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get html element reference/object without using getElementById

Status
Not open for further replies.

tiagompvieira

Programmer
Apr 17, 2008
5
0
0
PT

hi,

i'm trying to create a scrit that gets one html element but, i don't know the id.

i have something like:

...
<div id="firstDiv">
<script src="JsSource.js" type="text/javascript"></script>
</div>

<div id="firstDiv">
<script src="JsSource.js" type="text/javascript"></script>
</div>
...

--

the javascript file is suppost to change the current div

if i do:

"this.parent" I get a document object.
how can I in the javascript file get the div reference of the div it belongs without knowing the div id???
 
As far as I know you can't do this. Can you better explain what you are trying to accomplish I'm sure there are better ways to go about doing this.
 
i'll have lots of divs

...
<div id="firstDiv">
<script src="JsSource.js" type="text/javascript"></script>
</div>

<div id="secondDiv">
<script src="JsSource.js" type="text/javascript"></script>
</div>
...

the JsSource.js file will have a script that will change the current div, i will not know the div's id.

how do i in each script, obtain the current div reference in order to change it.

i can't use document.getElementById because I don't even know how many divs i'll have.

all the script knows is that it is inside the div.
 

yes, the javascript file (JsSource.js) is the same for all divs,

I know it could be better if I put the code in the header and call a function but, this is how I need the code.

do you have any ideia how to get the div were the script is called?

 
Tiago, you can run another script that will tag every div in the document with a id tag, that is a solution.

Another solution is using events, but i think for that you need to place the events inside the div thag.

When the event happens, it will pass the event argument, for example ev, and from ev you can get the element, i think.

If i'm wrong someone correct me.

If you can't change the div's in any way, you can run a script to addevents to every div in the document, the same way i said on the top of this reply.

Hope that helps.

Cheers.
 
You could try passing the ID of each DIV into the JS, e.g:

Code:
<div id="firstDiv">
   <script src="JsSource.js?divId=firstDiv" type="text/javascript"></script>
</div>

<div id="secondDiv">
   <script src="JsSource.js?divId=secondDiv" type="text/javascript"></script>
</div>

And then picking up on that in the JS.

I'm curious, though... why would each script need to know its container?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Ok well I was thinking about your problem one possible solution is to load an image, some small like a spacer and attach an onload event that will reference it's parent element on load. I would NOT recommend this, there has to be a better way to achieve what you are trying to accomplish but since you asked here goes.


html page
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
	</head>
	<body>
	</body>
	<div id="firstDiv">
	<script src="test.js" type="text/javascript"></script>
	</div>

	<div id="firstDiv">
	<script src="test.js" type="text/javascript"></script>
</div>
</html>


js code
Code:
document.write("<image src='images.gif' onload='alert(this.parentNode);' alt='test'>");


Another alternative would be to give your divs a class then look up a getElementsByClassName function.
 

I didn't know it was possible to send parameters to a javascript file with "?var=value".

my script can't recieve any paramenters, it has to find his parent.

I'm using a web development tool called outsystems, and don't have a good control over the elemens.

the reason that the script cant recieve any parameters is because this tool ( outsystems ) doesn't allow it.

is there any other way to get the current script div reference?

regards
 

Actually, there is another way,

it's quite simple,

I can't get the div's id but I can write inside the div, I don't need to change the current div, I can create whatever I want inside.

So now, I have a javacript function in the header that is called in every div reciving parameters that will built what I want inside every div.

This function builds ads, recievs links, image locations and id.

thank you all

Tiago
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top