views:

139

answers:

7
$('a.minimize').click(function() {
$($(this).attr('href')).hide();
});

<div class="drag" id="2">

<a href="#content" class="minimize" style="display: none;">2</a></p>
    <div id="content">1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br /></div>
</div>

i want to hide the #content by set the a href="" , why is not work???

any mistake?

A: 

hide() is not the right function to use on an attribute. It is used to set the visibility of DOM elements.

Try this instead:

$(this).attr('href', '');

womp
$(this).attr('href', ''); <<<this hide everything in the div drag
i want to hide the #content in the div drag,but don't hide the div drag
A: 

.hide() is different. You should have it like this.

$(this).attr('attr','value here')

$($(this).attr('href',''));

samer
A: 

Enclose your anchor tag event hanlder in document ready function.

$(function(){
    $('a.minimize').click(function() {
        $($(this).attr('href')).hide();
    });
});
rahul
+1  A: 

What your wanting is really unclear, at least to me. The answers you've gotten are right, in that if you want the href attribute to be empty, you don't use hide. But here is where I'm confused:

  1. You already have display:none set for that <a>, so that means it won't appear at all. So how is a user going to click on something they can't interact with?

  2. You are wanting to "hide" the <a> AFTER it gets clicked. If I had to guess you either want to:

    a. Make the link (with the 2 that's showing) go away after the user clicks it, or b. You want the user to only click on the link once, and after that, it becomes a dead link because it doesn't point to anything.

So, assuming I have the faintest idea of what you are going for, to make the link dead on click, follow the advice already given

 $('a.minimize').click(function() {
      $(this).attr('href', ''); 
 });

If you want the link to evaporate on click, go with:

 $('a.minimize').click(function() {
      $(this).hide(); 
 });  

Or am I totally missing the point?

Anthony
erm...i got 2 a href set, 1 is a.maximize, another 1 is a.minimizea.maximize set the a href in a.maximize to width 100% height 100% with jquery animateand the a.minimize set the a href in minimize(#content) to hide
rahul answer is work,but why only work once????example: after i click the a.maximize,the a.minimize become no function
rahul's only work's once because it is set up to run on document load, which is when the page first loads. If you want to use them over and over, you need to set a .click function for both hiding and unhiding and have them both set on document load. I would consider using `toggle` for this.
Anthony
$(function() { $('a.minimize').click(function() { $($(this).attr('href')).hide();});});this is not a .click function?
I put an example of toggle in my answer. Take a look at it.
Kyle Trauberman
please take a look at my answer
A: 

well.. from where to start.. first of all that wont ever work if you dont show the clickable element. so you would have to remove the style="display:none" like this:

<div class="drag" id="2">

<a href="#content" class="minimize" >2</a></p>
    <div id="content">1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br /></div>
</div>

second. the javascript wont ever work if you dont put in on script tags. and last and most important you cant try to get the element a.minimize before the browser load it. So you shold either or execute the script after the html of the element it's output or do it safely with the ready event of the element jQuery(document). so here is the code working:

<script>

jQuery(document).ready(function(){
$('a.minimize').click(function() {

$($(this).attr('href')).hide();
});
});
</script>

<div class="drag" id="2">

<a href="#content" class="minimize" >2</a></p>
    <div id="content">1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br /></div>
</div>
A: 

If I understand you correctly, you want to hide the div with the id of 'content' when you click the link with class 'minimize'.

This is the correct way to do it:

$("a.minimize").click(function() {
    $("#content").hide();
});

Or, if you want to toggle the status of the div with the id of 'content' when the link is clicked, try this:

$("a.minimize").click(function() {
    $("#content").toggle();
});
Kyle Trauberman
A: 
$(function() {
    $('a.maximize').click(function() {
        $($(this).attr('href')).animate({
        position: "absolute",
        top: 0,
        left: 0,
        height: '99.5%',
        width: '99.5%',
        opacity: 1,
  },1000)
});
});
$(function() {
    $('a.minimize').click(function() {
        $($(this).attr('href')).slideToggle();
});
});

this is my code , but why no function after click the a.maximize

You can enclose your two event handlers in the same document ready function itself. No need to use two of them.
rahul