Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Problem setting StyleClass with JavaScript
Old 08-17-2009, 12:40 PM Problem setting StyleClass with JavaScript
Novice Talker

Posts: 7
Name: The Chaval
Trades: 0
Good morning everyone,
I have a <UL> Html, and i want set a Style class call "current" each time you click on each <il> item , but it does not work.
I'll show you the code used to try accomplish this task:

- JavaScript Function:
<script>
// <![CDATA[
function setClassName(objId, className) {
document.getElementById(objId).className = className;
}
// ]]>
</script>

Function implementation into my proyect:
<ul id="saturday">
<li><a href="index.aspx" onclick="setClassName(this,'current')"><span>Inici o</span></a></li>
<li><a href="Default.aspx" onclick="setClassName(this,'current')"><span>Conta cto</span></a></li>
<li><a href="index.aspx" onclick="setClassName(this,'current')"><span>Estan cias</span></a></li>
<li><a href="index.aspx" onclick="setClassName(this,'current')"><span>Info</span></a></li>
<li><a href="index.aspx" onclick="setClassName(this,'current')"><span>Unipa go</span></a></li>
<li></li>
</ul>

it does not works.

your comments would be helpfull.
The_Chaval is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-17-2009, 12:55 PM Re: Problem setting StyleClass with JavaScript
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,522
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Well no it won't work>

What IS the point on setting a different class name on a link that is on a page that the browser will be LEAVING on the same click??
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-17-2009, 01:08 PM Re: Problem setting StyleClass with JavaScript
Novice Talker

Posts: 7
Name: The Chaval
Trades: 0
Is the same class name, just put in the time it was click the current <IL>, my goal is to indicate with a darker color which of the menu item(<il>) is selected.
The_Chaval is offline
Reply With Quote
View Public Profile
 
Old 08-17-2009, 01:10 PM Re: Problem setting StyleClass with JavaScript
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,522
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
on a page that the browser immediately LEAVES
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-17-2009, 02:29 PM Re: Problem setting StyleClass with JavaScript
Novice Talker

Posts: 7
Name: The Chaval
Trades: 0
no, i want put it in a page that i click, in the page tha it currently enter.

sorry, for my bad inglish.
The_Chaval is offline
Reply With Quote
View Public Profile
 
Old 08-17-2009, 03:23 PM Re: Problem setting StyleClass with JavaScript
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,522
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
then simply set the relevant navigation link to current when the page opens.

If your navigation is hardcoded it's simply a matter of typing it in and uploading the pages.
if your navigation is scripted it will need some IF statements in the code to determine which is current.

javascript can be done as well
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-17-2009, 03:26 PM Re: Problem setting StyleClass with JavaScript
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,522
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
then simply set the relevant navigation link to current when the page opens.

If your navigation is hardcoded it's simply a matter of typing it in and uploading the pages.
if your navigation is scripted it will need some IF statements in the code to determine which is current.

javascript can be done as well depending on how the navigation is set up
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-17-2009, 03:29 PM Re: Problem setting StyleClass with JavaScript
Novice Talker

Posts: 7
Name: The Chaval
Trades: 0
can you send me some code ( based on the source code send by me ) that i can serve as an example.
The_Chaval is offline
Reply With Quote
View Public Profile
 
Old 08-17-2009, 03:37 PM Re: Problem setting StyleClass with JavaScript
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Here's a tip I gave a while back: http://www.webmaster-talk.com/javasc...ive-links.html
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 08-17-2009, 04:14 PM Re: Problem setting StyleClass with JavaScript
Novice Talker

Posts: 7
Name: The Chaval
Trades: 0
thanks, this post was very useful for my.
The_Chaval is offline
Reply With Quote
View Public Profile
 
Old 08-17-2009, 04:40 PM Re: Problem setting StyleClass with JavaScript
Novice Talker

Posts: 7
Name: The Chaval
Trades: 0
I was testing your tips, but this does not work for me. see the following code, if you find something wrong:

function setClassName() {
var menu = document.getElementById("saturday").getElementsByTagName("a");
//creates an array out of each "a" element
for (var i = 0; i < menu.length; i++) {
if (menu[i].href == "http://" + window.location.hostname + window.location.pathname) {
// comparing each href vs this allows for query and hash variables
menu[i].className = "current";
// add class of "active" if there is a match
}
}
}

<ulid="saturday">
<li><ahref="page6"onclick="setClassName()"><span>Inicio</span></a></li>
<li><ahref="page5"onclick="setClassName()"><span>Contacto</span></a></li>
<li><ahref="page4"onclick="setClassName()"><span>Estancias</span></a></li>
<li><ahref="page3"onclick="setClassName()"><span>Info</span></a></li>
<li><ahref="pag2.aspx"onclick="setClassName()"><span>pag1.aspx</span></a></li>
<li></li></ul>
The_Chaval is offline
Reply With Quote
View Public Profile
 
Old 08-17-2009, 04:42 PM Re: Problem setting StyleClass with JavaScript
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Why are you attaching it to an onclick event? Just make it so that the script runs each time the page is loaded.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 08-17-2009, 04:50 PM Re: Problem setting StyleClass with JavaScript
Novice Talker

Posts: 7
Name: The Chaval
Trades: 0
Ok, that was the trouble, thanks a lot, it work fine.
The_Chaval is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Problem setting StyleClass with JavaScript
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.44969 seconds with 12 queries