473,396 Members | 1,942 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,396 software developers and data experts.

How do i getElementById("a") and add a class to it, using JavaScript,halfway there?

SM
Hello, i need to add a classname to an element <ausing JavaScript.
I've tried a couple of thing with no success.
This is an small piece of my code:
....
<li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this);
return false;">image 1</a></li>
<li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this);
return false;">image 2</a></li>
....

<img id="cover" src="<?php echo $cd_cover; ?>" alt="" /></div>
function show_picture(picture) {
var source = picture.getAttribute("href");
var cover = document.getElementById("cover");

cover.setAttribute("src", source);

//??????doesnt work
var el_a = document.getElementById("a");
el_a.className = "here";
}

Any ideas?

Thanks
Marco
Jun 27 '08 #1
7 2232
SM meinte:
Hello, i need to add a classname to an element <ausing JavaScript.
I've tried a couple of thing with no success.
This is an small piece of my code:
...
<li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this);
return false;">image 1</a></li>
<li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this);
return false;">image 2</a></li>
...

<img id="cover" src="<?php echo $cd_cover; ?>" alt="" /></div>
function show_picture(picture) {
var source = picture.getAttribute("href");
var cover = document.getElementById("cover");

cover.setAttribute("src", source);

//??????doesnt work
var el_a = document.getElementById("a");
el_a.className = "here";
}
As expected. Use other methods to get element references, like
getElementsByTagName() (returns a collection of elements with a given
name), or give your element a name or id and access it with
getElementById() or getElementsByName().

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jun 27 '08 #2
Gregor Kofler wrote:
SM meinte:
>Hello, i need to add a classname to an element <ausing JavaScript.
I've tried a couple of thing with no success.
This is an small piece of my code:
...
<li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this);
return false;">image 1</a></li>
<li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this);
return false;">image 2</a></li>
...

<img id="cover" src="<?php echo $cd_cover; ?>" alt="" /></div>
function show_picture(picture) {
var source = picture.getAttribute("href");
var cover = document.getElementById("cover");

cover.setAttribute("src", source);

//??????doesnt work
var el_a = document.getElementById("a");
el_a.className = "here";
}

As expected. Use other methods to get element references, like
getElementsByTagName() (returns a collection of elements with a given
name), or give your element a name or id and access it with
getElementById() or getElementsByName().

Gregor

Expanding upon what you said:

He did that for cover:

<img id="cover" src="<?php echo $cd_cover; ?>" alt="" />
var cover = document.getElementById("cover");
cover.setAttribute("src", source);

But he didn't have an element id="a" for the

var el_a = document.getElementById("a");
Jun 27 '08 #3
SM
On May 28, 5:20 pm, Gregor Kofler <use...@gregorkofler.atwrote:
SM meinte:
Hello, i need to add a classname to an element <ausing JavaScript.
I've tried a couple of thing with no success.
This is an small piece of my code:
...
<li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this);
return false;">image 1</a></li>
<li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this);
return false;">image 2</a></li>
...
<img id="cover" src="<?php echo $cd_cover; ?>" alt="" /></div>
function show_picture(picture) {
var source = picture.getAttribute("href");
var cover = document.getElementById("cover");
cover.setAttribute("src", source);
//??????doesnt work
var el_a = document.getElementById("a");
el_a.className = "here";
}

As expected. Use other methods to get element references, like
getElementsByTagName() (returns a collection of elements with a given
name), or give your element a name or id and access it with
getElementById() or getElementsByName().

Gregor

--http://photo.gregorkofler.at::: Landschafts- und Reisefotografiehttp://web.gregorkofler.com ::: meine JS-Spielwiesehttp://www.image2d.com ::: Bildagentur für den alpinen Raum
getElementByTagName doesn't work for me. getElementById does work ok.
I can't imagine having 30+ <aand give an 'id' tag for each. That's
just BAD programing in my opinion.
That's why i was looking for a general rule. Since i'm passing 'this'
in the function as a parameter, i was wondering if i could get the
parent or something similar and then add a class name. But then again,
i will need a way to undo that classname when i select another <a>.

I did more research and came up with this:

<ul id="nav_cd_imgs">
...
<li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this);
return false;">image 1</a></li>
<li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this);
return false;">image 2</a></li>
...
</ul>
var els_a =
document.getElementById("nav_cd_imgs").getElements ByTagName("a").length;

//testing purposes
alert(els_a);

//switch off all className in all <a>
for (i=0; i<els_a; i++) {
?????
}

//switch on the <aselected with a className
????
I just need to find how to get the <aselected. Any hints, meanwhile,
i'll continue searching the web and post the answer here if i find
something.
Thanks
Marco
Jun 27 '08 #4
SM meinte:
>--http://photo.gregorkofler.at::: Landschafts- und Reisefotografiehttp://web.gregorkofler.com ::: meine JS-Spielwiesehttp://www.image2d.com ::: Bildagentur für den alpinen Raum
Please, don't quote signatures.
getElementByTagName doesn't work for me.
It is getElement*s*ByTagName()
getElementById does work ok.
I can't imagine having 30+ <aand give an 'id' tag for each. That's
just BAD programing in my opinion.
What do you expect? How would your original approach have distinguished
between the different anchor-elements?
That's why i was looking for a general rule. Since i'm passing 'this'
in the function as a parameter, i was wondering if i could get the
parent or something similar and then add a class name. But then again,
i will need a way to undo that classname when i select another <a>.
I did more research and came up with this:

<ul id="nav_cd_imgs">
...
<li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this);
return false;">image 1</a></li>
<li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this);
return false;">image 2</a></li>
...
</ul>
Untested:

function show_picture(clicked_a) {
>
var els_a =
document.getElementById("nav_cd_imgs").getElements ByTagName("a").length;
var els_a =
document.getElementById("nav_cd_imgs").getElements ByTagName("a");
//testing purposes
alert(els_a);

//switch off all className in all <a>
for (i=0; i<els_a; i++) {
for(var i = els_a.length; i--;) {
if(els_a[i].className == "foo") {
els_a[i].className = "bar";
}
if(els_a[i] == clicked_a) {
els_a[i].className = "foo";
}
}
Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jun 27 '08 #5
SM
On May 28, 6:39 pm, Gregor Kofler <use...@gregorkofler.atwrote:
SM meinte:
--http://photo.gregorkofler.at:::Landschafts- und Reisefotografiehttp://web.gregorkofler.com::: meine JS-Spielwiesehttp://www.image2d.com ::: Bildagentur für den alpinen Raum

Please, don't quote signatures.
getElementByTagName doesn't work for me.

It is getElement*s*ByTagName()
getElementById does work ok.
I can't imagine having 30+ <aand give an 'id' tag for each. That's
just BAD programing in my opinion.

What do you expect? How would your original approach have distinguished
between the different anchor-elements?
That's why i was looking for a general rule. Since i'm passing 'this'
in the function as a parameter, i was wondering if i could get the
parent or something similar and then add a class name. But then again,
i will need a way to undo that classname when i select another <a>.
I did more research and came up with this:
<ul id="nav_cd_imgs">
...
<li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this);
return false;">image 1</a></li>
<li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this);
return false;">image 2</a></li>
...
</ul>

Untested:

function show_picture(clicked_a) {
var els_a =
document.getElementById("nav_cd_imgs").getElements ByTagName("a").length;

var els_a =
document.getElementById("nav_cd_imgs").getElements ByTagName("a");
//testing purposes
alert(els_a);
//switch off all className in all <a>
for (i=0; i<els_a; i++) {

for(var i = els_a.length; i--;) {
if(els_a[i].className == "foo") {
els_a[i].className = "bar";
}
if(els_a[i] == clicked_a) {
els_a[i].className = "foo";
}
Gregor,
Awesome! I've try it and it works perfectly! Many thanks.
My mistake, i didn't not explain correctly what i was searching for.
At the end, it all work out for the best!

Thanks again
Marco
Jun 27 '08 #6
Gregor Kofler wrote:
SM meinte:
>Hello, i need to add a classname to an element <ausing JavaScript.
I've tried a couple of thing with no success.
[...]
//??????doesnt work
var el_a = document.getElementById("a");
el_a.className = "here";
}

As expected. Use other methods to get element references, like
getElementsByTagName() (returns a collection of elements with a given
name), [...]
A given *tag* name, or better: element type.
getElementById() or getElementsByName().
Yes, *these* are about element's IDs and names.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Jun 27 '08 #7
Thomas 'PointedEars' Lahn meinte:
Gregor Kofler wrote:
>SM meinte:
>>Hello, i need to add a classname to an element <ausing JavaScript.
I've tried a couple of thing with no success.
[...]
//??????doesnt work
var el_a = document.getElementById("a");
el_a.className = "here";
}
As expected. Use other methods to get element references, like
getElementsByTagName() (returns a collection of elements with a given
name), [...]

A given *tag* name, or better: element type.
Typo. Could cause some confusion. Sorry.

Gregor

--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jun 27 '08 #8

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

Similar topics

2
by: Eric Pickup | last post by:
I'm having a problem with the following html (I've simplified it down to a simple test case). Could anyone point out why Mozilla is having problems with this (I've tried so many variations that I...
2
by: Dave Hammond | last post by:
I've got what should be a simple assignment of either an element value or a default string to a variable, but when the element doesn't exist I get an "Object required" error rather than an...
3
by: davidkarlsson74 | last post by:
Error: document.getElementById("folderMenu").cells has no properties File: http://www.volkswagen.se/tillbehor/js/foldermenu.js Rad: 49 The function activates different DIV:s, but doesn't seem to...
2
by: New guy.. please help | last post by:
"New guy.. please help" wrote: Hi, I'm a new kid onthe block.. I have a small company with a website that was developed in .net by a company for me. I want to work on it myself so I installed...
2
by: yourmallu | last post by:
Hi Friends, All we are knowing that we can get the element by document.getelementbyid("default_txtname").value. But If it is not on that page if i am taking header.ascx and placing text box...
3
by: rkhurana | last post by:
Hi I am writing a JSF application that uses some third party charts. While I can see the chart but the javascript that is supposed to calla function to update chart periodically has a problem. It...
4
by: gimme_this_gimme_that | last post by:
Hi, I have an onchange method for a select box that goes something like this (the select is in a form named aForm): function page_on_change() { pageElement = aForm.my_page_id;...
7
by: ankitoshniwal | last post by:
Hello, I have been having this problem for the whole day today, so even after i googled for the solution i was not able to get one, so i had to post to this forum. I had checked the solutions...
2
by: ChrisLA | last post by:
Hi; I've seen lots of discussion & disagreement on this issue, so any good explanation would be appreciated. Some people seem to think that "document.GetElementByID("MyName").submit(); should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.