1
I am trying to recover the id of a parent div of an input
<div id="parentDiv">
<input type="text" onfocus="mostra(this)">
</div>
Using the onFocus function, it returns me Undefined
function mostra()
{
alert($(this).parent().attr("id")); // output: undefined
}
However, by way of curiosity, this way works and returns me the right data
$(document).ready(function(){
alert($("input").parent().attr("id")); // output: parentDiv
});
What I’m doing wrong in using Parent() using onfocus?
The problem there is that the
this
there inalert($(this).parent(...
is not referring to this past as function parameter showcase() in HTML, but, yes to this of the context where it is inserted, so it returns Undefined, should take the parameter this passed in the function and then do the necessary logic:function mostra(this_referenciado) { alert($(this_referenciado).parent().attr("id")); })
– LeAndrade
@Leandrade sorry me.. I didn’t quite understand your placement. It would be possible, please, to show in code how the solution would be?
– Vanda P
Do not greet, do not thank and do not defer in the publications. See What kind of behavior is expected from users?
– Augusto Vasques