I am just learning js and was able to do a few things, but I was then recomended by Raffles that I should be using unobtrusive js, so I started working on it but I got stuck on the IE attachEvent, can someone point me in the right direction?
here is my code
<html>
<head>
</head>
<body>
name: <input id="test" name="test" type="text" size="50" maxlength="50"/>
<script type="text/javascript">
function format(event)
{
var target = event.target;
target.style.background = "#FFFF00";
}
var myvar=document.getElementById("test");
if (myvar.addEventListener)
{
myvar.addEventListener('change', format, false);
}
else if (myvar.attachEvent)
{
myvar.attachEvent('onblur', format);
}
</script>
</body>
</html>
Thanks, okay, that fixed the problem, but, what if I wanted to access the value of something, for instance, I was working on a different script where I would access the value of a field by doing target.value and it works just fine in firefox but it does nothing on IE, now, here is my code
<html>
<head>
<style type="text/css">
.errorcontainer
{
text-align:left;
width:auto;
height:auto;
position:fixed;
top:auto;
bottom:auto;
right:10px;
background:red;
color:white;
}
</style>
</head>
<body>
<input type="text" id="tester">
<p/>
<input disabled id="button" type="submit" value="submit" >
<div id="consoleoferrors" class="errorcontainer">
</div>
<script type="text/javascript">
function checkfieldforinvalidchars(event)
{
//Variables
var NOsymbolshere=new RegExp(/[ª!"·$%&()=?¿¡'º^*¨_:;,.-`+`+´ç]/);
var NOnumbershere=new RegExp(/\\d/);
var thefield=event.target || event.srcElement;
var thevalue=event.target.value || event.srcElement.value;
//Conditions
if (NOnumbershere.test(thevalue)==true)
{
document.getElementById("consoleoferrors").innerHTML=("We just found an error in your input:<br>No numbers are allowed in this field.<br>In this field you can only use letters from a-z and also accented vowels.<br>Please correct the highlighted field!");
thefield.style.background="#FFFF00";
}
else if (NOsymbolshere.test(thevalue)==true)
{
document.getElementById("consoleoferrors").innerHTML=("We just found an error in your input:<br>No symbols are allowed in this field.<br>In this field you can only use letters from a-z and also accented vowels.<br>Please correct the highlighted field!");
thefield.style.background="#FFFF00";
}
else if(thevalue.length<=1)
{
document.getElementById("consoleoferrors").innerHTML=("This field is mandatory, please complete it correctly!");
thefield.style.background="#FFFF00";
}
else if(thevalue.length>=15)
{
document.getElementById("consoleoferrors").innerHTML=("This field is mandatory and you have enetered too many characters, please fill it up correctly!");
thefield.style.background="#FFFF00";
}
else
{
thefield.style.background="#FFFFFF";
document.getElementById("consoleoferrors").innerHTML=("");
}
if (document.getElementById("consoleoferrors").innerHTML=="")
{
document.getElementById("button").disabled=false;
}
else
{
document.getElementById("button").disabled=true;
}
}
var apply=document.getElementById("tester");
if (apply.addEventListener)
{
apply.addEventListener('change', checkfieldforinvalidchars, false);
}
else if (apply.attachEvent)
{
apply.attachEvent('onchange', checkfieldforinvalidchars);
}
</script>
</body>
</html>
well I think I just figured how to achieve it, problem was that to obtain the value the way I was doing it, it would only work in either one of the browsers
here it would only work in FF
var thevalue=event.target.value || event.srcElement.value;
and here only in IE
var thevalue=event.srcElement.value || event.target.value;
what I did was, since I had already stored the field with
I think you have misunderstood my point. An event handler installed by IE using attachEvent, receives the event object as a parameter. Reading window.event doesn’t work, at least not under the circumstances of the code in this thread.
Earlier version of IE definitely do not pass the event to the function, so it would be good to see some documentation concerning when IE improved on that.
Regardless of that, reading window.event is only performed when evt is a falsy value, so there is no problem there.