Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnFocusOut coloring 1

Status
Not open for further replies.

jshurst

Programmer
Oct 27, 2004
1,158
US
I have a bunch of input buttons on a page (dynamically created by a asp:gridview). They all have the same id (id=btnName). When the button recieves focus I turn the font color red, then when the focus is lost it turns it back to aqua.

This is ok, except I really want the font to stay red until another "btnName" is clicked (not just loosing focus to anything on the page). Can someone help me out with a function that would do that?

Thanks in advance.

J
 
are they really "button" tags? or are the input tags with type of button? or are they anchor tags?

you'll need to loop through the set of "buttons" and style each one one way, except for the "active" button.

something like this:

Code:
<style type="text/css">
.active {
    color: red;
}
</style>

<script type="text/javascript"><!--
function blah(theButton) {
    var elems = theButton.form.elements;
    for ( var i = 0; i < elems.length; i++ ) {
        if ( elems[i].type == 'button' ) {
            elems[i].className = (elems[i].id <> theButton.id) ? '' : 'active';
        }
    }
}
//--></script>



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
They are input tags with type=button. I can change it to an anchor if it will be easier.

Could you tell me how to pass the button to the function?

Also can I just use something like...
{document.getElementById('btnName').style.color = 'aqua';}
in the loop, or do I need to create an "active" class?
 
you call the function like this:

Code:
<input type="button" name="b1" id="b1" onclick="blah(this);" />

yes, you can use inline styles. change that line in the function to something like this:

Code:
elems[i].style.color = (elems[i].id <> theButton.id) ? 'black' : 'red';



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Awesome man. My first JavaScript loop. Thanks for your help. Here is what I did.

Code:
function ColorText()
        {
            var elems = document.form1.elements;
            for ( var i = 0; i < elems.length; i++ ) 
            {
               if ( elems[i].id == 'btnName' ) 
                   {
                       elems[i].style.color = 'aqua';
                   }   
            }
         }

Then my button:
Code:
One more question.  How do I get the "element" if it is a link (anchor)?  I don't see it listed in the elements list when I loop through all of the elements.

Thanks,

J
 
links are not form elements, per se. i would do it in this manner:

1) give your links a surrounding parent element.
2) include the "return false" in the onclick (otherwise, when clicked, the browser will try to browse away from the page and inconsistent results occur.
3) change your function to find links in the parent element.

Code:
<div id="myParent">
  <a ...>blah</a>
  <a ...>blah</a>
  <a ... id="myLink" onclick="blah(this); return false;">blah</a>
  <a ...>blah</a>
  <a ...>blah</a>
</div>

Code:
var elems = document.getElementById("myParent").getElementsByTagName("a");



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Dude, you are the man. Worked perfectly! Thanks a ton.

J
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top