Irish SEO,  Marketing & Webmaster Discussion

 

Javascript loop

This is a discussion on Javascript loop within the Coding Help forums, part of the Webmaster Help category; I am looking for a way to change some font style from let's say bold to normal on some text ...


Go Back   Irish SEO, Marketing & Webmaster Discussion > Webmaster Help > Coding Help

Register Forum Rules FAQDonate Members List Calendar Search Today's Posts Mark Forums Read


Notices

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-02-2008, 03:56 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,010
Nominated 5 Times in 3 Posts
TOTW/F/M Award(s): 0
louie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud of
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default Javascript loop

I am looking for a way to change some font style from let's say bold to normal on some text links.
The problem is that I have more than one and they all start with some 2 letter text (example bellow)

Code:
<span id='yr123'>text</span>
<span id='yr1234'>text</span>
<span id='yr46545'>text</span>
<span id='yr897822'>text</span>
I have a js function that onclick will turn the text to bold, 12px, red
but i need to clear the style for all other spans that starts with yr...
Code:
function change_font(what){ 
document.getElementById(what).style.fontSize = "12"; 
document.getElementById(what).style.fontWeight = "bold";
document.getElementById(what).style.color = "#990000";
}
any suggestions?
__________________
:. Web Design & Development Web Design Ireland
:. Search Engines Optimization Search Engines Optimization
:. Directory Submission Directory Submission
:. News & Press Release Ireland GiveItSocks.com
:. Used Cars Ireland, Car Parts & Car Audio Cars For Sale, Car Parts & Accessories
:. I Have 2 Find It Directory SEF Directory
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 01-02-2008, 06:09 PM
Coder
 
Join Date: Jul 2007
Location: Galway
Posts: 53
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
davkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud of
Default

Could you use something like:

PHP Code:
function change_font(what){ 
    var 
allSpans document.getElementById("yr").getElementsByTagName("span");
    for(
i=0allSpans.lengthi++)
    {
        
allSpans.item(i).style.fontSize "30";
    }
    
document.getElementById(what).style.fontSize "10"
    
document.getElementById(what).style.fontWeight "bold";
    
document.getElementById(what).style.color "#990000";


PHP Code:
<div id="yr">
    <
span id='yr123'>text</span>
    <
span id='yr1234'>text</span>
    <
span id='yr46545'>text</span>
    <
span id='yr897822'>text</span>
</
div
This will reset the styles of all spans, and then set the one chosen....or did I misunderstand what you're trying to do?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3 (permalink)  
Old 01-02-2008, 06:18 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,010
Nominated 5 Times in 3 Posts
TOTW/F/M Award(s): 0
louie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud of
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default

It's a good start but is not going to match any id "yr" as they have some numbers picked at random after (e.g. span id='yr5645'
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4 (permalink)  
Old 01-02-2008, 06:54 PM
Coder
 
Join Date: Jul 2007
Location: Galway
Posts: 53
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
davkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud of
Default

So I did misunderstand!

Matching the spans that have an id beginning "yr", could be done with:

HTML Code:
    var allSpans = document.getElementsByTagName("span");
    for(i=0; i < allSpans.length; i++)
    {
        if (allSpans[i].id.substr(0, 2) == "yr")
            // do what ever style changes are needed...

    }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5 (permalink)  
Old 01-02-2008, 07:24 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,010
Nominated 5 Times in 3 Posts
TOTW/F/M Award(s): 0
louie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud of
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default

I cann't get my head around it.
all I have is the beginning of the string in this case "yr".
How am i supposed to reference to that particular span?
I tried:
Code:
document.getElementsByTagName("span").style.fontWeight = "normal";
and nothing
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6 (permalink)  
Old 01-02-2008, 07:28 PM
Coder
 
Join Date: Jul 2007
Location: Galway
Posts: 53
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
davkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud of
Default

Where's the onClick event being triggered?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7 (permalink)  
Old 01-02-2008, 07:30 PM
Coder
 
Join Date: Jul 2007
Location: Galway
Posts: 53
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
davkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud of
Default

Quote:
Originally Posted by louie View Post
I cann't get my head around it.
all I have is the beginning of the string in this case "yr".
How am i supposed to reference to that particular span?
I tried:
Code:
document.getElementsByTagName("span").style.fontWeight = "normal";
and nothing
Also, that code (getElementsByTagName) returns an array, so you'll need to loop through the array to change the individual elements, rather than than trying to set the style of the array (as you're doing above).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8 (permalink)  
Old 01-02-2008, 07:39 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,010
Nominated 5 Times in 3 Posts
TOTW/F/M Award(s): 0
louie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud of
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default

there is a link to see what I am talking about:

Alloy Wheels Ireland | New & Seconhand Tyres Fitment - Car Parts Ireland

is not an emergency but i love to have it done is just doing my head-in at the moment and can not put my finger on the situation. I need a break.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9 (permalink)  
Old 01-02-2008, 08:06 PM
Coder
 
Join Date: Jul 2007
Location: Galway
Posts: 53
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
davkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud of
Default

Will this do it:

Code:
function change_font(what){ 

    var start_of_id = what.substr(0, 2);    /*find out which group of ids it belongs to, for example, "mk" */

    var allSpans = document.getElementsByTagName("span");   // get all span elements
    for(i=0; i < allSpans.length; i++)    // loop through all the span elements
    {
        // identify the ones with starting with an id the same as the one that triggered the event
        if (allSpans[i].id.substr(0, 2) == start_of_id)
        {
            // reset any span elements starting with id="mk" to normal text
            allSpans[i].style.fontWeight = "normal";
            allSpans[i].style.color= "#000";
            allSpans[i].style.fontSize = "12";
        }
    }
    /*  Now all have normal text 
        Next customise the span element with id that was passed to function */
    document.getElementById(what).style.fontSize = "12"; 
    document.getElementById(what).style.fontWeight = "bold";
    document.getElementById(what).style.color = "#990000";
}
I don't know if that's the most efficient way of doing it, as you're looping through all the span elements on the page....but I think it works!!

Last edited by davkell; 01-02-2008 at 08:08 PM. Reason: Typo
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10 (permalink)  
Old 01-02-2008, 08:14 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,010
Nominated 5 Times in 3 Posts
TOTW/F/M Award(s): 0
louie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud of
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default

That did it. Thanks man. my hat to you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
javascript, loop

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
javascript 7aken Coding Help 1 03-04-2007 03:50 PM
javascript help 7aken Coding Help 2 20-02-2007 05:09 AM
Javascript menu bug? ButtermilkJack Coding Help 1 15-01-2007 02:21 PM
Javascript Captcha for use on forms louie Coding Help 8 23-10-2006 09:22 AM
Javascript Menu Help (again...)? ButtermilkJack Coding Help 18 25-04-2006 12:32 PM


All times are GMT +1. The time now is 06:43 AM.


Powered by: vBulletin Version 3.7.3, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
Hosted in Ireland by Blacknight - Test your ISP |Irish Hosting Directory| Armchair.ie|Logo by Eden Web Design|Avatars by Afterglow |Latest Blog Entries | VPS HostingAd Management by RedTyger

Search Engine Friendly URLs by vBSEO 3.2.0