Avatar of NewtonianB
NewtonianB
 asked on

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.
JavaScriptHTML

Avatar of undefined
Last Comment
NewtonianB

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
BurnieP

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
darren-w-

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
darren-w-

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");  
});
});
darren-w-

oops sorry forget what I said did not read question properly :)
NewtonianB

ASKER
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!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Justin Mathews

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

NewtonianB

ASKER
I want that div to be removed from the page
Justin Mathews

Including the contents i.e, <a href="" class="myBtn" rel="mydata"></a>
?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
NewtonianB

ASKER
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
Justin Mathews

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

NewtonianB

ASKER
amazing jmatix almost perfect, but instead of hiding or emptying the inner div, is there no such thing as delete div node?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SOLUTION
Justin Mathews

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
NewtonianB

ASKER
thanks alot!