Connecting Tech Pros Worldwide Forums | Help | Site Map

Using JS to change color of the contents of a span tag

Rob
Guest
 
Posts: n/a
#1: Jul 23 '05

Hi all,
Javascript is not my strong point but I have a little problem with a
function.

I have ASP code that displays form variables for x number of users.
Depending on the radio button they click, I highlite some text in red
over another form variable to show that it's mandatory. I use a <span>
tag for the text and a Javascript function to change the text color of
that user's mandatory field.

My Javascript call passes (i) to show which user I'm talking about but
the problem is I don't know how to change that user's text color.
Here's my function
<script language="JavaScript">
<!--
function ChangeTextColor(num){
membertype_1.style.color='red';
memberno_1.style.color='black';
lastname_1.style.color='black';
}

//-->
</script>
As you see, I'm not using the parameter "num" so it only works for the
first user. I need to know how to make it dynamic...I've tried

membertype_[+num+].style.color='red';

but that doesn't work.
Anyone know the syntax?

Thanks
Rob


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

McKirahan
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Using JS to change color of the contents of a span tag


"Rob" <rvenable@hotmail.com> wrote in message
news:4196692f$0$14507$c397aba@news.newsgroups.ws.. .[color=blue]
>
> Hi all,
> Javascript is not my strong point but I have a little problem with a
> function.
>
> I have ASP code that displays form variables for x number of users.
> Depending on the radio button they click, I highlite some text in red
> over another form variable to show that it's mandatory. I use a <span>
> tag for the text and a Javascript function to change the text color of
> that user's mandatory field.
>
> My Javascript call passes (i) to show which user I'm talking about but
> the problem is I don't know how to change that user's text color.
> Here's my function
> <script language="JavaScript">
> <!--
> function ChangeTextColor(num){
> membertype_1.style.color='red';
> memberno_1.style.color='black';
> lastname_1.style.color='black';
> }
>
> //-->
> </script>
> As you see, I'm not using the parameter "num" so it only works for the
> first user. I need to know how to make it dynamic...I've tried
>
> membertype_[+num+].style.color='red';
>
> but that doesn't work.
> Anyone know the syntax?
>
> Thanks
> Rob[/color]

Try the following:

<script type="text/javascript">
function ChangeTextColor(num) {
document.getElementById("membertype_"+num).style.c olor='red';
document.getElementById("memberno_"+num)style.colo r='black';
document.getElementById("lastname_"+num).style.col or='black';
}
</script>


McKirahan
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Using JS to change color of the contents of a span tag


"McKirahan" <News@McKirahan.com> wrote in message
news:bfuld.28810$V41.18417@attbi_s52...[color=blue]
> "Rob" <rvenable@hotmail.com> wrote in message
> news:4196692f$0$14507$c397aba@news.newsgroups.ws.. .[color=green]
> >
> > Hi all,
> > Javascript is not my strong point but I have a little problem with a
> > function.
> >
> > I have ASP code that displays form variables for x number of users.
> > Depending on the radio button they click, I highlite some text in red
> > over another form variable to show that it's mandatory. I use a <span>
> > tag for the text and a Javascript function to change the text color of
> > that user's mandatory field.
> >
> > My Javascript call passes (i) to show which user I'm talking about but
> > the problem is I don't know how to change that user's text color.
> > Here's my function
> > <script language="JavaScript">
> > <!--
> > function ChangeTextColor(num){
> > membertype_1.style.color='red';
> > memberno_1.style.color='black';
> > lastname_1.style.color='black';
> > }
> >
> > //-->
> > </script>
> > As you see, I'm not using the parameter "num" so it only works for the
> > first user. I need to know how to make it dynamic...I've tried
> >
> > membertype_[+num+].style.color='red';
> >
> > but that doesn't work.
> > Anyone know the syntax?
> >
> > Thanks
> > Rob[/color]
>
> Try the following:
>
> <script type="text/javascript">
> function ChangeTextColor(num) {
> document.getElementById("membertype_"+num).style.c olor='red';
> document.getElementById("memberno_"+num)style.colo r='black';
> document.getElementById("lastname_"+num).style.col or='black';
> }
> </script>
>[/color]

Oops, I forgot one of the dots; try this instead:

<script type="text/javascript">
function ChangeTextColor(num) {
document.getElementById("membertype_"+num).style.c olor='red';
document.getElementById("memberno_"+num).style.col or='black';
document.getElementById("lastname_"+num).style.col or='black';
}
</script>


Rob
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Using JS to change color of the contents of a span tag



Perfect. Thank you very much.

Rob


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Closed Thread


Similar JavaScript / Ajax / DHTML bytes