Why preventDefault not working? [duplicate] - php

I have the following code:
$(document).ready(function(){
$("div.subtab_left li.notebook a").click(function(event) {
event.preventDefault();
return false;
});
});
but when I click the element .. it doesn't prevent the default action .. Why?
and when modifying the code to:
$(document).ready(function(){
$("div.subtab_left li.notebook a").click(function() {
e.preventDefault();
alert("asdasdad");
return false;
});
});
it stops the default action but does not alert .. I couldn't find any answer on jQuery docs.
The full code goes like this:
$(document).ready(function(){
$('#tabs div.tab').hide();
$('#tabs div.tab:first').show();
$('#tabs ul li:first').addClass('active');
$('#tabs ul li a').click(function(){
$('#tabs ul li').removeClass('active');
$(this).parent().addClass('active');
var currentTab = $(this).attr('href');
$('#tabs div.tab').hide();
$(currentTab).show();
return false;
});
$("div.subtab_left li.notebook a").click(function(e) {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
alert("asdasdad");
return false;
});
});
and the HTML structure is:
<div id="tabs">
<ul id="nav">
<li><a id="tab1" href="#tab-1"></a></li>
<li><a id="tab2" href="#tab-2"></a></li>
<li><a id="tab3" href="#tab-3"></a></li>
<li><a id="tab4" href="#tab-4"></a></li>
</ul>
<div class="tab" id="tab-1">
<script type="text/javascript">$(document).ready(function(){$("ul.produse li").hover(function () {
$("ul.produse li").removeClass('active');$(this).addClass('active');}, function () {$(this).removeClass('active');});});
</script>
<div class="subtab_left">
<ul>
<li class="notebook">1</li>
<li class="netbook">2</li>
<li class="allinone">2</li>
<li class="desktop">2</li>
<li class="procesoare">2</li>
<li class="placi_video">2</li>
<li class="hdd_desktop">2</li>
<li class="tv_plasma">2</li>
<li class="tv_lcd">2</li>
<li class="telefoane_mobile last_item">2</li>
</ul>
</div>

Update
And there's your problem - you do have to click event handlers for some a elements. In this case, the order in which you attach the handlers matters since they'll be fired in that order.
Here's a working fiddle that shows the behaviour you want.
This should be your code:
$(document).ready(function(){
$('#tabs div.tab').hide();
$('#tabs div.tab:first').show();
$('#tabs ul li:first').addClass('active');
$("div.subtab_left li.notebook a").click(function(e) {
e.stopImmediatePropagation();
alert("asdasdad");
return false;
});
$('#tabs ul li a').click(function(){
alert("Handling link click");
$('#tabs ul li').removeClass('active');
$(this).parent().addClass('active');
var currentTab = $(this).attr('href');
$('#tabs div.tab').hide();
$(currentTab).show();
return false;
});
});
Note that the order of attaching the handlers has been exchanged and e.stopImmediatePropagation() is used to stop the other click handler from firing while return false is used to stop the default behaviour of following the link (as well as stopping the bubbling of the event. You may find that you need to use only e.stopPropagation).
Play around with this, if you remove the e.stopImmediatePropagation() you'll find that the second click handler's alert will fire after the first alert. Removing the return false will have no effect on this behaviour but will cause links to be followed by the browser.
Note
A better fix might be to ensure that the selectors return completely different sets of elements so there is no overlap but this might not always be possible in which case the solution described above might be one way to consider.
I don't see why your first code snippet would not work. What's the default action that you're seeing that you want to stop?
If you've attached other event handlers to the link, you should look into event.stopPropagation() and event.stopImmediatePropagation() instead. Note that return false is equivalent to calling both event.preventDefault and event.stopPropagation()ref
In your second code snippet, e is not defined. So an error would thrown at e.preventDefault() and the next lines never execute.
In other words
$("div.subtab_left li.notebook a").click(function() {
e.preventDefault();
alert("asdasdad");
return false;
});
should be
//note the e declared in the function parameters now
$("div.subtab_left li.notebook a").click(function(e) {
e.preventDefault();
alert("asdasdad");
return false;
});
Here's a working example showing that this code indeed does work and that return false is not really required if you only want to stop the following of a link.

Try this one:
$("div.subtab_left li.notebook a").click(function(e) {
e.preventDefault();
return false;
});

If e.preventDefault(); is not working you must use e.stopImmediatePropagation(); instead.
For further informations take a look at : What's the difference between event.stopPropagation and event.preventDefault?
$("div.subtab_left li.notebook a").click(function(e) {
e.stopImmediatePropagation();
return false;
});

Try this:
$("div.subtab_left li.notebook a").click(function(e) {
e.preventDefault();
});

If you already test with a submit action, you have noticed that not works too.
The reason is the form is alread posted in a $(document).read(...
So, all you need to do is change that to $(document).load(...
Now, in the moment of you browaser load the page, he will execute.
And will work ;D

i just had the same problems - have been testing a lot of different stuff. but it just wouldn't work.
then i checked the tutorial examples on jQuery.com again and found out:
your jQuery script needs to be after the elements you are referring to !
so your script needs to be after the html-code you want to access!
seems like jQuery can't access it otherwise.

Related

Save order in jquery sortable connected lists

My code is work fine.. but I like save the order in Mysql of the second column as soon as I make any changes... The PHP programming I can do, just do not know how to send the changes to php file
See my code ( complete code in: http://jsfiddle.net/helpinspireme/wMnsa/ )
<ul id="unassigned_list" class="connected_sortable">
<li class="ui-state-default"><div class="draggable_area">Item 1</div><div class="click_area">→</div><div class="cl"></div></li>
<li class="ui-state-default"><div class="draggable_area">Item 2</div><div class="click_area">→</div><div class="cl"></div></li>
</ul>
<ul id="recipients_list" class="connected_sortable">
<li class="ui-state-highlight"><div class="draggable_area">Item 3</div><div class="click_area">←</div><div class="cl"></div></li>
<li class="ui-state-highlight"><div class="draggable_area">Item 4</div><div class="click_area">←</div><div class="cl"></div></li>
</ul>
<script>
$("#unassigned_list, #recipients_list").sortable({
connectWith: ".connected_sortable",
items: "li",
handle: ".draggable_area",
stop: function(event, ui) {
updateLi(ui.item);
}
}).disableSelection().on("click", ".click_area", function() {
// First figure out which list the clicked element is NOT in...
var otherUL = $("#unassigned_list, #recipients_list").not($(this).closest("ul"));
var li = $(this).closest("li");
// Move the li to the other list. prependTo() can also be used instead of appendTo().
li.detach().appendTo(otherUL);
// Finally, switch the class on the li, and change the arrow's direction.
updateLi(li);
});
function updateLi(li) {
var clickArea = li.find(".click_area");
if (li.closest("ul").is("#recipients_list")) {
li.removeClass("ui-state-default").addClass("ui-state-highlight");
clickArea.html('←');
} else {
li.removeClass("ui-state-highlight").addClass("ui-state-default");
clickArea.html('→');
}
}
</script>
You can do it by adding id to elements and adding code below to sortable:
update: function() {
var order = $(this).sortable('toArray');
alert(order);
}
Online demo (jsFiddle)

Wordpress Menu + Bootstrap NavBar

I'm now stuck with this problem below, and I hope you can help me out. I would really appreciate it.
1st -- I created the Wordpress Menu (got it all worked).
2nd -- I implemented the Bootstrap classes (e.g nav navbar-nav, dropdown, dropdown-menu etc..) on the html tags of the Wordpress Menu. (got it and made it work somehow)
3rd -- To make the dropdowns work I used (preg_place) to replace the default (submenu) to (dropdown-menu) then to make the function work I used the (preg-replace) again to INSERT a class and data-toggle to the Anchor Links.
Now the problem is, the anchor links doesn't seem to function right at all.
check it out here.
(use the navs on top)
HEres the code on the function.php
function new_submenu_class($menu) {
$menu = preg_replace('/ class="sub-menu"/','/ class="dropdown-menu" /',$menu);
return $menu;
}
add_filter('wp_nav_menu','new_submenu_class');
function add_menuclass($ulclass) {
$ulclass = preg_replace('/<a/', '<a class="dropdown-toggle" data-toggle="dropdown" role="button"', $ulclass);
return $ulclass;
}
add_filter('wp_nav_menu','add_menuclass');
Thanks much guys!
And btw.. i used a jQuery for the 3rd level submenu. which is:
<script type="text/javascript">
$(document).ready(function() {
$('.navbar a.dropdown-toggle').on('click', function(e) {
var $el = $(this);
var $parent = $(this).offsetParent(".dropdown-menu");
$(this).parent("li").toggleClass('open');
if(!$parent.parent().hasClass('nav')) {
$el.next().css({"top": $el[0].offsetTop, "left": $parent.outerWidth() - 4});
}
$('.nav li.open').not($(this).parents("li")).removeClass("open");
return false;
});
});
</script>
In the function, I would try to change this line:
$('.navbar a.dropdown-toggle').on('click', function(e) {
For this one:
$('.navbar').on('click', 'a.dropdown-toggle', function(e) {
Since a.dropdown-toggle has been happended...
Maybe it was not present on "document ready", so the "delagation" syntax could help.

Reloading to homepage after hitting an empty anchor

Please have a look at http://iglica.dbox.pl/modx/ (clients graphical project)
Check "KURSY" on the menu - on the homepage everything is okay, the menu slides, I can pick something from the sub-menu, etc. Somehow, when I click on "KURSY" from every other place, MODX redirects me to the homepage.
The code responsible for the menu is in menu.min.js (I`ve bought it specially for this website - I'm not a jQuery familiar).
P.S.
I've tried this http://rtfm.modx.com/display/revolution20/Named+Anchor with no luck
P.S.
jQuery -
$(function () {
$('#mainNav ul li.slide a').click(function () {
var content = ($(this).parent().find("div").html());
$('#navWrapper #subNav #subNavContainer').fadeOut(100, function () {
$('#navWrapper #subNav #subNavContainer').html(content);
$('#navWrapper #subNav #subNavContainer').fadeIn(100);
$('#navWrapper').stop(false, false).animate({
'left': 0
}, 300)
})
});
$('#navWrapper').mouseleave(function () {
$('#navWrapper').stop(false, false).animate({
'left': -200
}, 300)
})
});
Try to replace the a tags which you don't want to do anything with span tags and see if your js code still works fine. Maybe you have to look at your css styles afterwards.
<li class="slide"><span>Kursy</span>
<div class="submenu">
<ul id="submenu">
<li>Dla dzieci</li>
<li>Dla młodzieży</li>
<li>Dla dorosłych</li>
</ul>
</div>
</li>
Ok, when slide functions are triggered by anchor may try a js workaround:
HTML
<li class="slide">Kursy
<div class="submenu">
<ul id="submenu">
<li>Dla dzieci</li>
<li>Dla młodzieży</li>
<li>Dla dorosłych</li>
</ul>
</div>
</li>
JS
(function ($) {
$(window).ready(function () {
$('.preventdefault').on("click", function (e) {
//Uncomment if the anchor is required on the url
//document.location.hash = $(this).attr('href');
return false;
});
});
}(jQuery));
Your code extended + adding html class:
$(function () {
$('#mainNav ul li.slide a').click(function (e) {
var content = ($(this).parent().find("div").html());
$('#navWrapper #subNav #subNavContainer').fadeOut(100, function () {
$('#navWrapper #subNav #subNavContainer').html(content);
$('#navWrapper #subNav #subNavContainer').fadeIn(100);
$('#navWrapper').stop(false, false).animate({
'left': 0
}, 300)
})
if ($(this).hasClass('preventdefault')) {
//Uncomment if the anchor is required on the url
//document.location.hash = $(this).attr('href');
e.preventDefault();
e.stopPropagation();
}
});
$('#navWrapper').mouseleave(function () {
$('#navWrapper').stop(false, false).animate({
'left': -200
}, 300)
})
});

Changing what is visible based on the url hash

Right now I have this list:
<ul id="list">
<li id="active"><img src="main home page/purhome.png"></li>
<li id="active"><img src="main home page/purinfo.png"></li>
<li id="active"><img src="main home page/purgyms.png"></li>
<li id="active"><img src="main home page/purcontact.png"></li>
</ul>
And this JQuery:
$(document).ready(function(){
$("#home-link").click(function(){
$("#main_info2").hide();
$("#main_info").show();
});
$("#info-link").click(function(){
$("#main_info").hide();
$("#main_info2").show();
});
$("#gyms-link").click(function(){
$("#main_info").hide();
$("#main_info2").show();
});
$("#contact-link").click(function(){
$("#main_info").hide();
$("#main_info2").show();
});
});
When I click on the home link, the URL shows indexp.php#home, which is what i want it to show. This happens for all the links. However if i refresh the page after i click on the info link for example, the main_info div is shown, but the browser still says index.php#info. I want to read the hash somehow to show the correct div depending on what the url says.
So for example if I click on the info link, the url shows index.php#info. When I refresh it says the same thing but the main_info div shows when the main_info2 div is supposed to be showing.
function getHash() {
var hash = window.location.hash;
return hash.substring(1); // remove #
}
So I have that code. Would I do something like this?
if hash = 'info' // if url says index.php#info
show main_info2
hide main_info
if hash = nothing // So if url says index.php
show main_info
hide main_info2
Any ideas?
EDIT**
I'm trying to do something like this to make it work:
var currentValue = window.location.hash.substr(1)
$(document).ready(function() {
$("home-link").click(function(){
if(currentValue == "home") {
$("#main_info2").hide();
$("#main_info").show();
}
});
$("info-link").click(function(){
if(currentValue == "info") {
$("#main_info").hide();
$("#main_info2").show();
}
});
});
Although that is not working, can someone check the code?
You can use this jQuery plugin to listen to hash changes:
https://github.com/cowboy/jquery-hashchange
I would recommend using a plugin if you want your code to work cross browser because this hasn't been standardized yet. Especially older browsers (IE6) require extensive hacks.
After some discussion about the different answers and problem, I came up with this solution:
$(document).ready(function(){
$(window).hashchange(function(){
var hash = location.hash;
if(window.location.hash.substr(1) == "home") {
$("#info-container").hide();
$("#gyms-container").hide();
$("#contact-container").hide();
$("#home-container").show();
}
else if(window.location.hash.substr(1) == "info") {
$("#home-container").hide();
$("#gyms-container").hide();
$("#contact-container").hide();
$("#info-container").show();
}
else if(window.location.hash.substr(1) == "gyms") {
$("#home-container").hide();
$("#info-container").hide();
$("#contact-container").hide();
$("#gyms-container").show();
}
else if(window.location.hash.substr(1) == "contact") {
$("#home-container").hide();
$("#info-container").hide();
$("#gyms-container").hide();
$("#contact-container").show();
}
$('#list a').each(function(){
var that = $(this);
that[ that.attr( 'href' ) === hash ? 'addClass' : 'removeClass' ]( 'selected' );
});
})
$(window).hashchange();
});
This work beautifully.
This uses the hashchange plugin mentioned above so make sure you insert the script into your head before using this function.
Here is bit of code to help you get started if you use https://github.com/cowboy/jquery-bbq:
$(document).ready(function(){
$("#home-link").click(function(){
$("#main_info2").hide();
$("#main_info").show();
});
$("#info-link").click(function(){
$("#main_info").hide();
$("#main_info2").show();
});
$("#gyms-link").click(function(){
$("#main_info").hide();
$("#main_info2").show();
});
$("#contact-link").click(function(){
$("#main_info").hide();
$("#main_info2").show();
});
$(window).bind( 'hashchange', function(e) {
url = $.deparam.fragment();
url && $( 'a[href="#' + url + '"]' ).trigger('click');
});
$(window).trigger('hashchange');
});

How to make a secondary document ready function?

Ok this is my issue if anyone can help, please.
I have a href that div id to switch content - I would like to add another document ready function javascript without conflicting with make tab I have already.
Example of make tab already:
<script type="text/javascript">
{literal}
$(document).ready(function(){
function makeTabs(selector) {
var tabContainers = $(selector + ' > div');
tabContainers.removeClass("selected").filter(':first').addClass("selected");
galleryRendered = false;
$(selector + ' > ul a').click(function () {
tabContainers.removeClass("selected");
tabContainers.filter(this.hash).addClass("selected");
$(selector + ' > ul a').removeClass('selected');
$(this).addClass('selected');
if (this.hash == '#Pictures' && !galleryRendered)
{
var galleries = $('.pictures > .ad-gallery').adGallery({
effect : 'slide-hori',
enable_keyboard_move : true,
cycle : true,
animation_speed : 400,
slideshow: {
enable: false
},
callbacks: {
init: function() {
this.preloadImage(0);
this.preloadImage(1);
this.preloadImage(2);
}
}
});
galleryRendered = true;
}
if (this.hash == '#OnTheMap') document.getElementById("Map").map.onContainerChanged();
return false;
}).filter(':first').click();
}
makeTabs('.tabs');
});
{/literal}
</script>
Want to create a second one so I can create tabs inside of an existing div id area/content to switch from photo to video to youtube.
<div class=".tabs"><ul><li>[[Photo]]</li><li>[[Youtube]]</li><li>[[Video]]</li></ul><div id="photo">Test</div><div id="tube">Test</div><div id="vid">Test</div></div>
This will be inside a div id that already exist that uses the first tab creator shown above.
In jQuery you just have to do this:
$(function(){
// code here
});
$(function(){
// more code here
});
Every function declared like this will be executed on domready.

Categories