Add attribute to every contol in project

tluyet

New member
Joined
Jan 26, 2009
Messages
2
Programming Experience
5-10
what is the best way to add an attribute to every <input type='text'> item in a project? {ex. <input type='text' onclick="alert('textbox was clicked.')"}

I tried useing javascript and it would only add the attribute to the last one in the loop.

I don't know how to do it in vbscript or jscript.

I have tried several things in vb.net but I can't seem to get it to work.
 

tluyet

New member
Joined
Jan 26, 2009
Messages
2
Programming Experience
5-10
figured it out

this may not be the best way to do it but here is how I accomplished it:

javascript (added this to the onload attribute of my body tag. what this does is add the attribute onkeydown=getkey(); to every textbox and textarea) :
Code:
function Test () {
  var elements = document.getElementsByTagName('input');
  for (var i = 0; i < elements.length; i++) { 
   if (elements[i].type == 'text') {
      elements[i].onkeydown=function() {getKey();};
    }
  }
  elements = document.getElementsByTagName('textarea');
  for (var i = 0; i < elements.length; i++) { 
         elements[i].onkeydown=function() {getKey();};
  }
}
however I didn't know what textbox/textarea it was coming from. so I added this bit of javascript to my getkey function. theID is the name of the textbox/textarea:

Code:
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;
theId=targ.id;
this solved my problem. I hope it helps someone else. by the way this has only been tested in IE. the only one I was concerened about.
 
Top Bottom