views:

162

answers:

6
<a rel="abc" href="#mydiv">link</a>
<div id="mydiv">content</div>

If rel="abc", find element with ID that matches href value and hide it.

My try:

$('[rel*=abc]').attr("href").hide();

Thanks for your help!

A: 
var identifier = $('a[rel="abc"]').attr('href');
$('#'+identifier').hide();

I think this may solve your problem. I didn't test it, though.

Jacob R
Looks like you got -1 because your selector will be "##mydiv"
Mike Robinson
Wasn't sure if that would have been the case or not. Thanks for the tip.
Jacob R
+2  A: 
$('a[rel=abc]').click( function(event){
    event.preventDefault();
    $(event.target.href.substr(event.href.indexOf('#'))).hide();
});

Hides the appropriate element if such a link is clicked.

edit: tested

RamboNo5
Might want to add event.preventDefault() into that. Or at least return false.
Mike Robinson
Of course. Thanks!
RamboNo5
+2  A: 
$("a[rel='abc']").click(function(event){
  event.preventDefault();
  var val = $(this).attr("href");
  $("div"+val).hide();
});
Jonathan Sampson
+5  A: 
$( $("a[rel='abc']").attr("href") ).hide();
Jeff Sternal
Nice, neater than what I was just writing. Perhaps put this in a $(document).ready(function(){}) block.
tsdbrown
(Note will fail if your IDs contain `.` or `:`.)
bobince
+1  A: 

$('a[rel=abc]').each(function() { $(this.href.substr(this.href.indexOf('#'))).hide(); });

Some error checking would be good too.

Mike
+1  A: 
$("a[rel=abc]").each(function(i, ele) {
    $(ele.hash).hide();
});

or if you want that to happen on click

$("a[rel=abc]").click(function(e) {
    e.preventDefault();
    $(this.hash).hide();
});
jitter