Solved

jquery click get self

Posted on 2010-11-12
13
327 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTML5 save .Dat to server side 20 45
Checkout Page Input Field not aligned 1 21
ASP server side get value 15 19
alert(innerHTML); 7 7
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
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…

786 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