Home › Forums › JavaScript › getting error undefined in the following statement
- This topic is empty.
-
AuthorPosts
-
July 25, 2013 at 11:55 pm #144644
gowda24
ParticipantHi ,
Need to know why this error occurs
<div class=”productlftSec”>
</img>
<span class=”spantext” >Demo1</span>
</img>
<span class=”spantext” >Demo1</span><div>
<div class=”productrgtSec”>
<div id=”product1″ class=”proDesc” style=”display:block;”>
<p style=”margin-top:20px;line-height:22px;font-size:14px;”>
Demo1
</p>
</div>
<div id=”product2″ class=”proDesc” style=”display:block;”>
<p style=”margin-top:20px;line-height:22px;font-size:14px;”>
Demo2
</p>
</div>
<div>function showproduct(){
var id = $(this).attr(‘id’);
alert(id);
// this line gives id undefined.
}$(‘.showproduct’).click(function(){
var id = $(this).attr(‘id’);
alert(id);
});Why i get this error even though both refer the same statements
[Link to codepen…](http://codepen.io/gowda24/pen/ojdzg “Check the code “)
July 26, 2013 at 1:06 am #144649Leonhard Ortner
ParticipantHi,
just try:
$(‘.showproduct’).click(function(){
var $id = $(this).attr(‘id’);
alert($id);
});you forgot to add js to codepen… i think its a problem with your variable-name “id” may be used somewhere just use “$id” oder “xyz_id”…
EDIT: Sorry should have read your post more carefully… :/ my answer want help u…
wbr
leoJuly 26, 2013 at 1:44 am #144653gowda24
ParticipantHi Leo ,
I am having the same code , and it works fine,
But calling other way using onclick
function showproduct(){
var id = $(this).attr(‘id’);
alert(id);
// this line gives id undefined.
}This statement is giving error.
I just want to know y this code gives error and not the other one.July 26, 2013 at 2:19 am #144659gowda24
ParticipantHi
Check this out.
The first link returns the id of the anchor tag,
But the second link doesnt.
[Link](http://jsfiddle.net/lakshminarayan/HdUuJ/1/ “test”)
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.