Something causing interference - javascript

So I've got the following code that works in jsfiddle, but not on the actual website, which leads me to believe that the only way I'm going to get it working is for someone with more experience than I to look through the source and see what's interfering.
<script>
$(document).ready(function(){
var $elements = $('body').children('div[class^=class]').on('click', function () {
$elements.removeClass('classname')
.not('.' + this.className)
.addClass('classname');
});
});
</script>
Website: http://sinfulgurotesque.tumblr.com/recs
Edit: I've removed a section of code from the website that had nothing to do with this part. (It was the deprecated code, and it didn't offer much in terms of functionality anyways.)

You're using jQuery 1.10.1 in your page, and the error you're getting:
jquery.style-my-tooltips.js:26 Uncaught TypeError: undefined is not a
function
is referring to the .live() function used by the plugin which was removed in jQuery 1.9
$(".smt-current-element").live("mouseout mousedown click",function(){

It looks like you're calling depracated JS functions. I got:
TypeError: $(...).live is not a function
when opening your page. Try changing .live() to .on()
Like so
$("body").on("mouseout mousedown click", ".smt-current-element", function(){...

Related

Polymer fix messing up Javascript

I'm new to Polymer and as far as I've read about it, it isn't compatible with Mozilla and Safari or it has issues. I've read in StackOverflow that adding
addEventListener('WebComponentsReady', function() {
});
would help the browsers cope up with the code. Now, I've tried it on my code it works. The content is displaying properly in Mozilla, however, it messes up the Javascript that I wrote along with Polymer. I tried two options, the first one
addEventListener('WebComponentsReady', function() {
Polymer({
is: "main-header"
}); });
I did this and there are still error logs on the console while if I wrap the whole script, it wouldn't work as well. Example:
addEventListener('WebComponentsReady', function() {
Polymer({
is: "main-header"
});
// extra code here
});
I think wrapping addEventListener to the whole code is also causing the problem. Any ideas how to fix or are there any other viable options than adding an event listener to the code?
Try using Polymer-CLI
It comes with some polyfills out of the box.
I'm not sure whitch ones but it does include the one your inquiring about.
https://www.polymer-project.org/1.0/docs/tools/polymer-cli
It looks like using
addEventListener('WebComponentsReady', function() {
});
is messing up my Javascript because it has a conflict with
addEventListener('HTMLImportsReady', function() {
});
I had to remove it in order for the script to work properly.

Toggleclass not working, altough should

This is pretty basic, still doesn't work... I'm trying to change the class of my button, which has the id "nav_list". I wrote the script:
var specialSection = document.getElementById("nav_list");
specialSection.onclick = function() {
alert("what");
$('#nav_list').toggleClass('active');
};
I get the "what" alert, but the class isn't toggled. What am I missing? Thanks in advance!
toggleClass is a jQuery method, for use it you must include jQuery in your page.
Like:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
At the moment if you check the console you must see an error like (demo: http://jsfiddle.net/IrvinDominin/GyaBY/):
ReferenceError: Can't find variable: $
Avoid mixing jQuery and javascript event binding so your code will look like:
$('#nav_list').click(function() {
alert("what");
$(this).toggleClass('active');
});
Demo: http://jsfiddle.net/IrvinDominin/GyaBY/2/

jQuery onclick placer doing nothing

I have an input on my index page:
<input type="button" name="goReplaceAll" value="Go" id="allReplaceGo" style="cursor:hand;" />
And an onclick call from jquery:
$(document).ready(function() {
$('#allReplaceGo').click(replaceAll);
});
function replaceAll(){ alert('a'); }
When I click the button, nothing happens. When I instead use onclick="replaceAll()" within the HTML, I get an error: Uncaught ReferenceError: replaceAll is not defined
I am unable to figure out a connection between these errors, or any possible cause.
The code is currently live at http://www.texturepacker.net
Edit: Looks like I get two different results in Firefox and Chrome, Chrome does nothing while Firefox alert('a')'s once the page loads. Now I'm just plain confused?
Edit: Seemingly an unrelated syntax error later in my code was breaking the call. Now replaceAll() is called when the dom loads, my question is now why isn't replaceAll() being launched onclick and instead once the dom loads, am I missing something obvious?
Look like a syntax error here...
Line 196 in you script file says
$(this).("src",tmpSrc);
It is supposed to be
$(this).attr("src",tmpSrc);
Fix that and it should be all fine..
Also on line 7
$('#allReplaceGo').live("click",replaceAll());
is supposed to be
$('#allReplaceGo').live("click",replaceAll);
Also as of version 1.7 .live() is deprecated. try using .on() instead
That happens, probably, because the function replaceAll is outside the jQuery scope.
Try to put her inside the $(document).ready scope.
Your code should look like
$(document).ready(function() {
$('#allReplaceGo').click(function(){
replaceAll()
});
});
function replaceAll(){ alert('a'); }

mootools and jQuery conflict

I've got a simple form in a page that is loading Mootools and JQuery. JQuery is in no conflict mode, which seems like it ought to cause no problems.
There's a form element called "name"--
<input class="required" id="sendname" name="sendname" value="">
And I'm trying to attach a click event to it using Mootools to update something else when the name box is clicked:
$('sendname').addEvent('click', function(e){
// do stuff.
});
The problem is that the click event never gets added.
This error appears on load:
Uncaught TypeError: Cannot call method 'addEvent' of null
When I try to interact with the element in a js console, I get the following error:
> $('sendname').setProperty('value', 'test');
TypeError: Object sendname has no method 'setProperty'</strike>
EDIT: the previous was fixed by loading a newer Mootools. However, the click event still isn't functioning properly, though it throws no errors or warning.
This code works fine in almost any situation I've used it in. I assume there's some issue with jQuery conflicting, but the fact that the $ notation works seems to confirm that noConflict mode is operational. Any ideas?
You are targetting the element wrongly... I think this has nothing to do with a possible conflict.
In this case you need to add the hash for an id or a period for a class, like this:
$('#sendname').addEvent('click', function(e){
// do stuff.
});
Notice the # in #sendname
MooTools has Dollar Safe mode that automatically releases the $ to other libs as long as MooTools is loaded last.
If Dollar Safe mode is active, you need to use:
document.id('SomeElementID').someMethod()
What is happening in the example you are giving, is that you're using jQuery to select the element, and a MooTools method on the result. The thing is, jQuery returns the jQuery object which has no such 'addEvent' method on it. MooTools works on the actual elements so you need to select them with a MooTools query method first: $ == document.id or $$ == document.search
You can cache document.id to a var for convenience if you want:
var $M = document.id;
$M('sendname').addEvent(...)
As described in the comments to the OP, the issue was the load-order of the jQuery/Mootools scripts. The jQuery noConflict was being loaded too late, and causing problems. Please see jsfiddle -- http://jsfiddle.net/uSwzL/1/
Without any problem even loading jquery.js after other $ based library loaded:
<script>$=function(){alert('hell');}</script>
<script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
<script>
$.noConflict();
$();
alert(jQuery.trim(' hello '));
</script>
Even in php framework html template:
<script>
function on_doc_ready()
{jQuery(function($)
{$( "#sortable" ).sortable();
$( "#sortable" ).disableSelection();
}
);
}
function load_jquery_ui()
{var s2=document.createElement('scr'+'ipt');
s2.setAttribute('onload', 'on_doc_ready();');
s2.src='/app/idm/statics/jQuery/js/jquery-ui-1.10.0.custom.min.js';
document.getElementsByTagName('head')[0].appendChild(s2);
}
function load_jquery_after_mootools()
{var s1=document.createElement('scr'+'ipt');
s1.src='/app/idm/statics/jQuery/js/jquery-1.9.0.js';
s1.setAttribute('onload', '$.noConflict();load_jquery_ui();');
document.getElementsByTagName('head')[0].appendChild(s1);
}
load_jquery_after_mootools();
<script>

jQuery effect on iframe parent document

Just wondering if anyone else has experienced this or knows why I am getting an error. I'm using javascript from within an iframe to call a parent dom element then use jQuery UI's effect core to shake it. Here is an example:
$(document).ready(function(){
if ($("form").length>0)
{
$("form").submit(function(){
var oParentDoc = $(parent.document).find("div#element");
var action = $(this).attr("action");
var postdata = $(this).serialize();
$(oParentDoc).addClass("loading");
$.post(action,postdata,function(data){
$(oParentDoc).removeClass("loading").effect("shake",{"times":3,"distance":10},60);
});
return false;
});
}
});
It works without the effect, but when I use an effect it gives me this error:
uncaught exception: [Exception...
"Component returned failure code:
0x80040111 (NS_ERROR_NOT_AVAILABLE)
[nsIDOMCSSStyleDeclaration.getPropertyValue]"
nsresult: "0x80040111
(NS_ERROR_NOT_AVAILABLE)"
Thanks in advance for any insight :)
I'm not sure if this will work but you could try setting up a bind event in the parent, then in the iFrame try triggering that event in the parent.
Parent JavaScript
$(document).ready(function(){
$('#iframe').bind('shakeFrame',function(){
$('#iframe').effect("shake",{"times":3,"distance":10},60);
});
});
iFrame JavaScript
$(document).ready(function(){
$(parent.document).find('#iframe').trigger('shakeFrame');
});
It could be that your Iframe's specifications are defined. Therefore not allowing any movement / change with the Iframe?
Have you loaded the necessary jQuery files into the parent document as well as the iframe document?
UPDATE: A little research on the internet indicates that the 'effect' code is probably calling 'getPropertyValue' and Firefox is claiming that method doesn't exist. I know - not very helpful. Unfortunately I believe you're going to have to debug the 'effect' code to find out more specifically whose bug this is, or perhaps find that its a limitation of the iframe scenario you're trying to run.

Categories