473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

DOM howto


Hello. I was wondering how to wrap the image contained with a div
container with an anchor tag. Could somebody provide me with the
code necessary to achive this using DOM-based scripting?

[Before]
<div id="foo">
<img src="image.png" />
</div>

[After]
<div id="foo">
<a href="bar.html"><img src="image.png" /></a>
</div>
Thanks in advance.

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Sep 1 '06 #1
5 2990
Koncept wrote on 01 sep 2006 in comp.lang.javascript:
Hello. I was wondering how to wrap the image contained with a div
container with an anchor tag. Could somebody provide me with the
code necessary to achive this using DOM-based scripting?

[Before]
<div id="foo">
<img src="image.png" />
</div>

[After]
<div id="foo">
<a href="bar.html"><img src="image.png" /></a>
</div>
var foo = document.getElementById('foo');
foo.innerHTML = '<a href="bar.html">' + foo.innerHTML + '</a>';

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 1 '06 #2
[Before]
<div id="foo">
<img src="image.png" />
</div>

[After]
<div id="foo">
<a href="bar.html"><img src="image.png" /></a>
</div>
In article <Xn********************@194.109.133.242>, Evertjan.
<ex**************@interxnl.netwrote:
var foo = document.getElementById('foo');
foo.innerHTML = '<a href="bar.html">' + foo.innerHTML + '</a>';
Thanks for the super-fast response!

I am looking for an explaination of how to do this using
document.createElement just to get an understanding of how to wrap
node(s).

I can assume this much...

var div = document.getElementById("foo");
var anchor = document.createElement("a");

anchor.setAttribute("href", [dynamically assigned] );
anchor.setAttribute("title","Click here to ...");

// now I need to wrap the anchor node that was just created around
// div#foo. How make this worky? ;)

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Sep 1 '06 #3
Koncept wrote on 01 sep 2006 in comp.lang.javascript:
>[Before]
<div id="foo">
<img src="image.png" />
</div>

[After]
<div id="foo">
<a href="bar.html"><img src="image.png" /></a>
</div>

In article <Xn********************@194.109.133.242>, Evertjan.
<ex**************@interxnl.netwrote:
>var foo = document.getElementById('foo');
foo.innerHTML = '<a href="bar.html">' + foo.innerHTML + '</a>';

Thanks for the super-fast response!

I am looking for an explaination of how to do this using
document.createElement just to get an understanding of how to wrap
node(s).
Sorry I never use those if I can, I make too many mistakes there.
>
I can assume this much...

var div = document.getElementById("foo");
Do not name a variable with an element name, consider those words
"reserved", at least for your own sanity.

var foo = document.getElementById("foo");

The above, however, is perfectly legal,
even works in IE if you do not forget the "var"

var anchor = document.createElement("a");

anchor.setAttribute("href", [dynamically assigned] );
anchor.setAttribute("title","Click here to ...");
I think the setAttribute is not necessary anymore, just do:

anchor.href = ....;
anchor.title = 'Hi!';
// now I need to wrap the anchor node that was just created around
// div#foo. How make this worky? ;)
Eh? Perhaps with an

anchor.innerHTML = foo.innerHTML
or
anchor.appendChild(document.createTextNode(foo.inn erHTML));

Never tried that. My solution seems simpler.

Gurus whatsay?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 1 '06 #4
In article <Xn********************@194.109.133.242>, Evertjan.
<ex**************@interxnl.netwrote:
I think the setAttribute is not necessary anymore, just do:

anchor.href = ....;
anchor.title = 'Hi!';
// now I need to wrap the anchor node that was just created around
// div#foo. How make this worky? ;)

Eh? Perhaps with an

anchor.innerHTML = foo.innerHTML
or
anchor.appendChild(document.createTextNode(foo.inn erHTML));

Never tried that. My solution seems simpler.

Gurus whatsay?
Cheers. Will try these methods. Thanks for the help. :)

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Sep 1 '06 #5
Koncept wrote:
>[Before]
<div id="foo">
<img src="image.png" />
</div>

[After]
<div id="foo">
<a href="bar.html"><img src="image.png" /></a>
</div>

In article <Xn********************@194.109.133.242>, Evertjan.
<ex**************@interxnl.netwrote:
>var foo = document.getElementById('foo');
foo.innerHTML = '<a href="bar.html">' + foo.innerHTML + '</a>';

Thanks for the super-fast response!

I am looking for an explaination of how to do this using
document.createElement just to get an understanding of how to wrap
node(s).

I can assume this much...

var div = document.getElementById("foo");
var anchor = document.createElement("a");

anchor.setAttribute("href", [dynamically assigned] );
anchor.setAttribute("title","Click here to ...");

// now I need to wrap the anchor node that was just created around
// div#foo. How make this worky? ;)
Add all the children of div to anchor in order, then add the anchor to
the div:

while (div.firstChild){
anchor.appendChild(div.firstChild);
}
div.appendChild(anchor);
Untested, but should work. Depending on the content of div, you might
end up with invalid HTML - not everything that can be a child of a div
can be a child of an anchor.
--
Rob
Sep 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or...
4
by: Josef Sachs | last post by:
Is Andrew Kuchling's regex-to-re HOWTO available anywhere? I've found the following (dead) links on various Web pages: http://py-howto.sourceforge.net/regex-to-re/regex-to-re.html...
5
by: alejandro lapeyre | last post by:
How can I load / parse an HTML file with .NET? Thanks! Best regards, Alejandro Lapeyre
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
0
by: Mark Harrison | last post by:
HOWTO: Integrating Posgresql queries into an event loop. Mark Harrison mh@pixar.com May 27, 2004 Problem ------- The commonly used postgresql APIs will block until completed.
8
by: Topper | last post by:
Hello. I have simple web folders structure: -ROOT - BIN WebService.dll WebService.asmx I need to use my WebService.dll not in bin folder - for example, in ROOT. How do i this? How can i do...
0
by: Andrew Dalke | last post by:
Years ago I wrote the Sorting mini-howto, currently at http://www.amk.ca/python/howto/sorting/sorting.html I've had various people thank me for that, in person and through email. It's...
4
by: Marco Meoni | last post by:
Hi. I read the Gordon McMillan's "Socket Programming Howto". I tried to use the example in this howto but this doesn't work. The code is class mysocket: '''classe solamente dimostrativa -...
7
by: dmitrey | last post by:
hi all, can anyone explain howto get function from module, known by string names? I.e. something like def myfunc(module_string1, func_string2, *args): eval('from ' + module_string1 + 'import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.