Hello.
I have a javascript object with one attribute (name) and one method
(showName). Each object instance appends a DIV to the document's body. I
want that when the user clicks over the DIVS the alert shows the name of
the instance which has created the DIV.tancia que ha creado ese DIV:
See online:
http://www.tel.uva.es/~jpozdom/metodo.html
When the user clicks on the first square the message box should show:
Object1. When the user clicks on the second square the message box
should show: Object2.
I don't know how to do that. The following sentence doesn't work.
this.div.onclick=this.muestraNombre;
Thanks. And excuse my poor english.
<html>
<head>
<script type="text/javascript">
function Objeto(nombre) {
this.nombre=nombre;
this.div=document.createElement('div');
this.div.style.border='1px solid #000';
this.div.style.width='10px';
this.div.style.height='10px';
this.div.onclick=this.muestraNombre;
// Esto no funciona, la función se ejecuta pero
// la referencia this se pierde y aparece 'undefined'
var body=document.getElementsByTagName('body').item(0) ;
body.appendChild(this.div);
}
function MuestraNombre() {
alert(this.nombre);
}
Objeto.prototype.muestraNombre=MuestraNombre;
</script>
</head>
<body>
<script type="text/javascript">obj1=new Objeto('Objeto1');</script>
<p></p>
<script type="text/javascript">obj2=new Objeto('Objeto2');</script>
</body>
</html>