como dice el título, me sigo "indefinido" cuando trato de obtener el atributo id de un elemento, básicamente lo que quiero hacer es reemplazar un elemento con un cuadro de entrada cuando el valor es "otro".
Aquí está el código:
function showHideOther(obj){
var sel = obj.options[obj.selectedIndex].value;
var ID = $(this).attr("id");
alert(ID);
if(sel=='other'){
$(this).html("<input type='text' name='" + ID + "' id='" + ID + "' />");
}else{
$(this).css({'display' : 'none'});
}
}
El HTML:
<span class='left'><label for='race'>Race: </label></span>
<span class='right'><select name='race' id='race' onchange='showHideOther(this);'>
<option>Select one</option>
<option>one</option>
<option>two</option>
<option>three</option>
<option value="other">Other</option>
</select>
</span>
Es probablemente algo pequeño que no estoy notando, ¿qué estoy haciendo mal?
¡Gracias por adelantado!
"esto" siempre se referirá al objeto en el que está trabajando. Y puede ser utilizado en cualquier lugar. Por supuesto, si sabes lo que estás haciendo. – GerManson
en lugar de '$ (this) .href', puede simplemente' this.href' y también 'obj.id' ... btw,' obj.attr ('id'); 'debe ser' $ (obj) .attr ('id'); 'basado en OP ... – Reigel