Skip to main content

jQuery function is not working as i planned, DIVs just disappear on click



I have a script here, and it works amazing, except for one small detail. It basically runs as click function that fades in and out certain DIVs on the click of a link.





The problem is, however, that when you click ANYWHERE on the page, it removes the DIV and leaves an empty content area until you click one of the links. Obviously this is a problem.





I can't use the exact code I'm using for NDA reasons, but here is same setup I am using with just some plain text in the divs.





Just click the links to see the functionality, and then click anywhere else on the page (in the div, in the white space, whatever) and watch the div just disappear. I'm know that that is what the javascript is calling, but I don't know how to disable the click in the divs so this doesn't happen. Also, how to do that so it doesn't disable the link that are within that div.





Any help is greatly appreciated. This website is a life saver.





This is the original code I got off of this site edited to show my very basic layout. Basically a bunch of links that are inline and when clicked, they change a section of DIVS to another section of divs:







<html>



<head>



<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>

<script type="text/javascript">

$(document).ready(function() {

var gLastH = null;

var gLastId = null;



$('.toggle').click(function(e) {

$('.toggleh:visible').fadeOut('slow');

gLastId = $(this).attr('id');

console.log('#' + gLastId + 'h');

gLastH = $('#' + gLastId + 'h');

$(gLastH).fadeIn('slow');

e.stopPropagation();

});



$('*').click(function(e) {

if ($(this).attr('id') != gLastId) {

$(gLastH).fadeOut('slow');

}



e.stopPropagation();

});

});

</script>



</head>



<body>

<div id="menubar">



<a href="#" onclick="return false;" class="toggle" id="toggle1" style="display: inline;">

text here</a>

<a href="#" onclick="return false;" class="toggle" id="toggle2" style="display: inline;">

text here</a>

<a href="#" onclick="return false;" class="toggle" id="toggle3" style="display: inline;">

text here</a>

<a href="#" onclick="return false;" class="toggle" id="toggle4" style="display: inline;">

text here</a>



<div class="toggleh" id="toggle1h">

some description in here I suppose

</div>



<div class="toggleh" id="toggle2h" style="display: none;">

some description in here I suppose #2dsfds sdfdsfa sdf

</div>



<div class="toggleh" id="toggle3h" style="display: none;">

some description in here I suppose #3dffdfasdfdsfdfasf

</div>



<div class="toggleh" id="toggle4h" style="display: none;">

some description in here I suppose #4 dfdafa

</div>

</div>



</body>



</html>




Comments

  1. $('*').click(... sets up a click handler for ALL elements (*). You can remove this function.

    ReplyDelete
  2. That's what this code does:

    $('*').click(function(e) {
    if ($(this).attr('id') != gLastId) {
    $(gLastH).fadeOut('slow');
    }

    e.stopPropagation();
    });


    It selects all of the elements in the document (including html and head and body and attaches a click handler to it. When you click on the document, it compares the id, which likely is empty and then fades out the displayed div.

    Demo without that code.

    ReplyDelete
  3. Not the real expert
    But this seems to be the faulty part

    $('*').click(function(e) {...............}

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.