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 ...
| |||||||
| Register | Forum Rules | FAQ | Donate | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| |||||
| 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> 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";
}
__________________ :. 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 |
| ||||
| Could you use something like: PHP Code: PHP Code: |
| |||||
| 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' |
| ||||
| 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...
}
|
| |||||
| 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";
|
| ||||
| Where's the onClick event being triggered? |
| ||||
| 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). |
| |||||
| 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. |
| ||||
| 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";
}
Last edited by davkell; 01-02-2008 at 08:08 PM. Reason: Typo |
| |||||
| That did it. Thanks man. my hat to you. |
| Tags |
| javascript, loop |
| Thread Tools | |
| Display Modes | |
|
|
| ||||
| 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 |