0

I can't seem how to figure as to how to attach an event (dynamically) to textboxes and select boxes:

Error: Object Required, line 19

<!DOCTYPE html>
<html>
    
    <head>
        <script type="text/javascript">
            window.onload = function fnOnLoad() {
                var t = document.getElementsByTagName('SELECT');

                var i;
                for (i = 0; i < t.length; i++) {
                    alert(t[i].id)

                    document.getElementById(t[i].id).attachEvent('onfocus', function () {
                        document.getElementById(this.id).style.backgroundColor = "#FFFF80"
                    })
                    document.getElementById(t[i].id).attachEvent('onblur', function () {
                        document.getElementById(this.id).style.backgroundColor = "#FFFFFF"
                    })

                }
            }
        </script>
    </head>
    
    <body>
        <input id="t1" type="text">
        <input id="t2" type="text">
        <input id="t3" type="text">
        <br>
        <br>
        <select id="d1"></select>
        <select id="d2"></select>
        <select id="d3"></select>
    </body>

</html>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jason Kelly
  • 2,539
  • 10
  • 43
  • 80

2 Answers2

0

why don't you use jquery and use the bind function: for example:

var element = $('#myElementId') // equivalent to document.getElementById
element.bind('click', function() {
    // when the element is clicked, it will do what ever you write here
    alert('i was clicked');
}); 

you can put this code inside the onload or onready function with jquery like this:

$(function() {
    var element = $('#myElementId') // equivalent to document.getElementById
    element.bind('click', function() {
        // when the element is clicked, it will do what ever you write here
        alert('i was clicked');
    }); 
}); // this is the onload

or

$(document).ready(function() {
    var element = $('#myElementId') // equivalent to document.getElementById
    element.bind('click', function() {
        // when the element is clicked, it will do what ever you write here
        alert('i was clicked');
    }); 
}); // this is the on ready
Mr T.
  • 4,278
  • 9
  • 44
  • 61
  • BTW - you can bind all the select elements by getting them with jquery selector so instead of `$('#myElementId')` you would use `$('select')` to get all the select elements in the document – Mr T. Oct 16 '12 at 18:43
-1
<script type="text/javascript">

function fnOnLoad() {

    var t = document.getElementsByTagName('INPUT');
    for (var i = 0; i < t.length; i++) {
            t[i].onfocus = function() { this.style.backgroundColor = '#FFFFC4';};
            t[i].onblur = function() { this.style.backgroundColor = '#FFFFFF'; };
    }

    var d = document.getElementsByTagName('SELECT');
    for (var i = 0; i < d.length; i++) {
            d[i].onfocus = function() { this.style.backgroundColor = '#FFFFC4'; };
            d[i].onblur = function() { this.style.backgroundColor = '#FFFFFF'; };
    }

}
</script>
Jason Kelly
  • 2,539
  • 10
  • 43
  • 80