0

I'm trying to make a button that will hide a specific -- and then replace it with another hidden . However, when I test the code, everything fires correctly except for the .removeClass which contains the "display: none."

Here is the code:

 <script type="text/javascript">
    $(document).ready(function(){
        var webform = document.getElementById('block-webform-client-block-18');
        var unmarriedbutton = document.getElementById('unmarried');
        var buyingblock = document.getElementById('block-block-10');

        $(unmarriedbutton).click(function () {
            $(buyingblock).fadeOut('slow', function() {
                $(this).replaceWith(function () {
                    $(webform).removeClass('hiddenbox')
                });
            });
        });
    });
    </script>

The CSS on 'hiddenbox' is nothing more than "display: none.'

There is a with the id of unmarried, which when clicked fades out a div and replaces it with a hidden div that removes the class to reveal it. However, the last part doesn't fire -- everything else does and functions properly. When I look at in the console too, it shows no errors.

Can someone please tell me where the error is? Thanks!

Edit: I may be using the wrong function to replace the div with, so here's the site: http://drjohncurtis.com/happily-un-married. If you click the "download the book" button, the the div disappears and is replaced correctly with the div#block-webform-client-block-18. However, it remains hidden.

Darthfuzzy
  • 31
  • 1
  • 7

2 Answers2

1

The function you pass to replaceWith has to return the content you want to replace it with. You have to actually return the content.

I don't know exactly what you're trying to accomplish, but you could use this if the goal is to replace it with the webform object:

$(this).replaceWith(function () {
       return($(webform).removeClass('hiddenbox'));
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Maybe I'm using the wrong function then, however here is the actual site: http://drjohncurtis.com/happily-un-married. If you click the "DOWNLOAD THE BOOK!" it should return a with the div#block-webform-client-block-18 and it does, it just remains hidden (and that's after I used your solution). – Darthfuzzy Feb 09 '13 at 04:22
  • I made the change on the site with the return and it still does not work -- it still remains empty. – Darthfuzzy Feb 09 '13 at 04:28
  • it is `$(document.getElementById('block-webform-client-block-18')` that's creating the bug – mikakun Feb 09 '13 at 04:30
  • @mikakun I don't think so. – Ilia Choly Feb 09 '13 at 04:36
  • It's not that creating the bug. I changed the var webform like you suggested and the the first function failed. – Darthfuzzy Feb 09 '13 at 04:38
-2

NB, use jquery !

    var webform = $('#block-webform-client-block-18');
    var unmarriedbutton = $('#unmarried');
    var buyingblock =$('#block-block-10');
    unmarriedbutton.click(function () {
        buyingblock.fadeOut('slow', function() {
            $(this).replaceWith( webform.removeClass('hiddenbox'));

        });
    });

Was too fast, i believe it's the way you select your object (getelementbyid) then you create a jquery object from it... -> use jquery API

mikakun
  • 2,203
  • 2
  • 16
  • 24