• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

jquery click get self

$('.myDiv').click(function(){  
        var myVar = $('.myClass').attr("rel");  
}

This is wrong, I need to specify the myclass for the one inside myDiv not the entire dom document, how do I specify the one that was clicked inside click function(), there could  be any number of myDiv on the page.
0
NewtonianB
Asked:
NewtonianB
  • 5
  • 4
  • 3
  • +1
3 Solutions
 
BurniePCommented:
$('.myDiv').click(function(){  
        var myVar = $(this).find('.myClass').attr("rel");  
}

0
 
darren-w-Commented:
I think his is it

$('.myDiv').click(function(){  
        var myVar = $(this).attr("rel");  
}
0
 
darren-w-Commented:
Elaborating on this, I think you may need to iterate through all the items to add listeners to them something like:

$('.myDiv').each(function(){
$(this).click(function(){  
        var myVar = $(this).attr("rel");  
});
});
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
darren-w-Commented:
oops sorry forget what I said did not read question properly :)
0
 
NewtonianBAuthor Commented:
Great thank you quick related question

I have a bunch of divs on my site

<div class="searchDiv">
  <a href="" class="myBtn" rel="mydata"></a>
</div>
<div class="searchDiv">
  <a href="" class="myBtn" rel="mydata"></a>
</div>
<div class="searchDiv">
  <a href="" class="myBtn" rel="mydata"></a>
</div>


The jquery I was asking for is to capture the rel in whichever href was clicked.
How can I remove searchDiv from the HTML in jquery also? Basically I'm implementing a delete button, I'd like the searchDiv parent to be removed for the corresponding anchor tag.
IMPORTANT NOTE: My anchor link is actually very deep inside searchDiv so It wouldn't be so smart to call a bunch of parent tags.
Basically my question is how can I find the closest parent with the corresponding class?

Please let me know if you would like me to ask this in a different thread. Thanks!
0
 
Justin MathewsCommented:
To get the rel attribute, you can use the script below. But what do you mean by 'remove searchDiv from the HTML'?
$(document).ready(function(){
	$('.myBtn').click(function(){
		var myVar = $(this).attr("rel");
                   alert(myVar);
	});
});

Open in new window

0
 
NewtonianBAuthor Commented:
I want that div to be removed from the page
0
 
Justin MathewsCommented:
Including the contents i.e, <a href="" class="myBtn" rel="mydata"></a>
?
0
 
NewtonianBAuthor Commented:
Yes I want the closest parent div called searchDiv to be removed from the html where the child <a href="" class="myBtn" rel="mydata"></a> was clicked
0
 
Justin MathewsCommented:
Try either one.
<script type="text/javascript">
<!-- menu
$(document).ready(function(){
	$('.myBtn').click(function(){
		alert($(this).attr("rel"));
		$(this).parents('.searchDiv').css("display", "none");
	    return false;
	});
});
//-->
</script>


OR


<script type="text/javascript">
<!-- menu
$(document).ready(function(){
	$('.myBtn').click(function(){
		alert($(this).attr("rel"));
		$(this).parents('.searchDiv').html("");
	    return false;
	});
});
//-->
</script>

Open in new window

0
 
NewtonianBAuthor Commented:
amazing jmatix almost perfect, but instead of hiding or emptying the inner div, is there no such thing as delete div node?
0
 
Justin MathewsCommented:
You can use remove() as:

<script type="text/javascript">
<!-- menu
$(document).ready(function(){
      $('.myBtn').click(function(){
            alert($(this).attr("rel"));
            $(this).parents('.searchDiv').remove();
          return false;
      });
});
//-->
</script>
0
 
NewtonianBAuthor Commented:
thanks alot!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now