I know how to access an element by it´s id, but how could I get the id of an element
Let´s say I have
var id;
function getid()
{
var get=get the id of the element that triggered the function;
id=get
alert(id);
}
<input type="submit" id="test" onclick="getid()">
this is the code I have, but I want to use it for different fields, it works right now, but if the first change to the fields is made on field4 then the script will not work, I found a way around and made it work in my form but I am looking for a way to get the id of an element so I dont have to be looking for “ways around” in the future and also to make my code smaller.
Does anyone know how I can retrieve the id of an element on a onchange or other event?
<html>
<body>
<script type="text/javascript">
var x=1;
var ids="test";
function fixuplow()
{
var myvar=(document.getElementById(ids+x).value);
var process=(myvar.toLowerCase());
var final=(process.charAt(0).toUpperCase() + process.slice(1));
document.getElementById(ids+x).value=final;
x++;
}
</script>
something to up and lower case: <input type="input" id="test" onchange="fixuplow()">
</body>
</html>
I just read another article about unobstrusive javascript and I think I now better understand it, I will try to do that with the previous code to see if I can figure out how to make it work, but if you could also give me some hints on it I would really apreciate it.
The main hint is to put all your JavaScript at the bottom of the body, not in the head. Then it will run after the HTML has loaded, which is what you want.
Give it a go and if it doesn’t work, post what you did and I’ll help you.
well about the event listener I was not very familiar, thanks, but I am feeling retarded at the moment about the getelementbyid which I been using the whole day, I think I should take a break and relax before doing any more reading / testing
Thank you for all your help, I will go through the links you posted.