/    Sign up×
Community /Pin to ProfileBookmark

Grabbing border colour

how do you grab the border colour of an element if the colour has been defined in css at the top of the page?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@HaganeNoKokoroSep 28.2004 — This seems to work for me in Netscape7 and Firefox 1.0PR, but not IE6
<i>
</i>&lt;script type="text/javascript"&gt;
function getBColor(id) {
var stylestuff = document.getElementsByTagName("style")[0].firstChild.data;
var start=stylestuff.indexOf(id);
var end=stylestuff.indexOf("}", start);
var estyle=stylestuff.substring(start, end);
var estart=estyle.indexOf("border-color");
var ans="";
if(estart&gt;=0) {
ans=estyle.substring(estyle.indexOf(":", estart)+1, estyle.indexOf(";", estart));
}
return ans;
}
&lt;/script&gt;

Of course, this only gets you what was defined inside the first style tag in the document (presumably inside the head). If you change the border color somewhere else, then it will still give you the one that was defined in the head.

I sure hope there's a better way than this.
Copy linkTweet thisAlerts:
@artemisauthorSep 28.2004 — well that certainly works but its not very pretty, ? , and it has many drawbacks. if there isnt a solution then i will have to hard code it in, but i would have liked it to have been dynamic.

Thanks anyhoo though!
Copy linkTweet thisAlerts:
@HaganeNoKokoroSep 28.2004 — Not very pretty? It's ugly as sin! As a side note, it could be made to be dynamic-ish: you could use this in onload to set the element.style.borderColor attribute of elements you want, then just refer to that after. But it's still ugly.

example:

[upl-file uuid=d78611b8-09aa-4431-8c4b-62158a9be1c1 size=1kB]bordercolor.html.txt[/upl-file]
Copy linkTweet thisAlerts:
@artemisauthorSep 29.2004 — if anyone cares i just created an extra argument in the field and passed the border colour through it.
×

Success!

Help @artemis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...