Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

jquery click get self

Posted on 2010-11-12
13
Medium Priority
?
337 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 800 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 400 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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:Justin Mathews
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:Justin Mathews
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:Justin Mathews
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:Justin Mathews
Justin Mathews earned 800 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

618 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