Solved

jquery click get self

Posted on 2010-11-12
13
332 Views
Last Modified: 2012-05-10
$('.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
Comment
Question by:NewtonianB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 16

Accepted Solution

by:
BurnieP earned 200 total points
ID: 34120013
$('.myDiv').click(function(){  
        var myVar = $(this).find('.myClass').attr("rel");  
}

0
 
LVL 13

Assisted Solution

by:darren-w-
darren-w- earned 100 total points
ID: 34120016
I think his is it

$('.myDiv').click(function(){  
        var myVar = $(this).attr("rel");  
}
0
 
LVL 13

Expert Comment

by:darren-w-
ID: 34120037
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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 13

Expert Comment

by:darren-w-
ID: 34120050
oops sorry forget what I said did not read question properly :)
0
 

Author Comment

by:NewtonianB
ID: 34121023
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
 
LVL 16

Expert Comment

by:jmatix
ID: 34121343
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
 

Author Comment

by:NewtonianB
ID: 34121622
I want that div to be removed from the page
0
 
LVL 16

Expert Comment

by:jmatix
ID: 34121813
Including the contents i.e, <a href="" class="myBtn" rel="mydata"></a>
?
0
 

Author Comment

by:NewtonianB
ID: 34121835
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
 
LVL 16

Expert Comment

by:jmatix
ID: 34121878
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
 

Author Comment

by:NewtonianB
ID: 34121936
amazing jmatix almost perfect, but instead of hiding or emptying the inner div, is there no such thing as delete div node?
0
 
LVL 16

Assisted Solution

by:jmatix
jmatix earned 200 total points
ID: 34121961
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
 

Author Closing Comment

by:NewtonianB
ID: 34121980
thanks alot!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question