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

R

Rob

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
 
M

McKirahan

Rob said:
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

Try the following:

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

McKirahan

McKirahan said:
Rob said:
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

Try the following:

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

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top