I have this code: http://jsfiddle.net/AH4As/3/
It works in the fiddle, but on my site, it does not. I'm getting this error in Web Inspector
Tracking.js:4TypeError: 'undefined' is not an object (evaluating '$('a').attr('onClick').replace')
Does anybody know what wrong?
Here's my source code: http://jsfiddle.net/AH4As/24/
UPDATED:
$(document).ready(function(){
$('a').attr('onClick', $('a').attr('onClick').replace("window.open('", "window.open('http://example.com/"));
});
http://jsfiddle.net/AH4As/10/
And here it is with an alert to show you the code has been changed:
http://jsfiddle.net/AH4As/11/
Try:
$('a').removeAttr('onclick').click(function(){window.open('http://example.com');return false;});
What about:
$(document).ready(function(){
$('a').removeAttr('onclick');
$('a').click(function(){
window.open('http://www.example.com', '','location=1,menubar=1,scrollbars=1,status=1,resizable=1,width=635,height=460');
});
});
You won't be able to (reliably) replace on the onclick with jQuery 1.4:
'function' === typeof $('a').attr('onclick');
And Functions don't have replace methods:
'undefined' === typeof $('a').attr('onclick').replace;
You're getting a function because jQuery is returning the property value rather than the attribute value.
The confusion from this is why jQuery added the .prop() method in 1.6 -- so it's clear that you want the attribute value, not the property value:
// with jQuery 1.6+
'string' === typeof $('a').attr('onclick');
Now, if you're not able to update jQuery currently, you can try inserting a toString before the replace:
$('a').attr('onclick').toString().replace(...)
On a side-note: for better coupling of the original and adjusted value, you may look at using an alternate syntax for .attr():
$('a').attr('onclick', function (i, value) {
return value.toString().replace('window.open(\'', 'window.open(\'http://example.com/'));
});
Other anchors on the page made the Query fail, in a nutshell this was my solution:
<script type="text/javascript">
$(document).ready(function(){
$('a[onclick^="window.open(\'TrackPackage.asp"]').attr('onClick', $('a[onclick^="window.open(\'TrackPackage.asp"]').attr('onClick').replace("window.open('", "window.open('http://eastcoasttvs.com/"));
});
</script>
Related
This is my code:
$.get("http://www.roblox.com/catalog/", function(onWebsite) {
console.log($(onWebsite).find('.name.notranslate')[0].attr("href"));
});
Whenever I run it, it does not log anything to the console, all it says is: "Object {readyState: 1}". However, if I remove the .attr("href"), it works. Is there something wrong with my syntax?
Since .attr() is a jQuery function you need to use it with jQuery object.
Use
$(onWebsite).find('.name.notranslate').attr("href")
As per your current code $(onWebsite).find('.name.notranslate')[0] will return you underlying DOM element which doesn't have .attr() method.
You can use href property or Element.getAttribute() method
$(onWebsite).find('.name.notranslate')[0].href
OR
$(onWebsite).find('.name.notranslate')[0].getAttribute('href')
Maybe try:
$.get("http://www.roblox.com/catalog/", function(onWebsite) {
console.log(onWebsite.find('.name.notranslate')[0].attr("href"));
});
onWebsite must not be wrapped with the jQuery selector as its a return from a function.
Lets try this approach:
var a;
$.get("http://www.roblox.com/catalog/", function(onWebsite) {
a = $(onWebsite).find('.name.notranslate')[0];
console.log(a);
});
You will get
<a class="name notranslate" href="/Headless-Horseman-item?id=134082613" title="Headless Horseman">Headless Horseman</a>
So var a is the HTML Tag with href attribute. HTML tags doesn't have jQuery methods - only jQuery objects selected from DOM or "wrapped/selected HTML"
Here you can:
reference .href to get href
wrap a in jQuery to get: $(a).attr('href')
So end code would be (variant 1 is better):
var a;
$.get("http://www.roblox.com/catalog/", function(onWebsite) {
a = $(onWebsite).find('.name.notranslate')[0].href
console.log(a);
});
or without creation of temp variable
$.get("http://www.roblox.com/catalog/", function(onWebsite) {
console.log($(onWebsite).find('.name.notranslate')[0].href);
});
I'm teaching myself AJAX to AJAXify my site. In my template, I have the following JS code to get some JSON data from a view then append the data to a div.
function filter(type) {
$.getJSON(
'/activity_stream/global-activity-stream/',
{xhr: "true", filter: type},
function(data) {
$('.mainContent').children().remove();
$(data).appendTo('.mainContent');
});
}
$(".btn").click(function () {
filter("recent");
});
}
I think my view is returning proper JSON but now data is not being added to the .mainContent div.
It gives this error:
Uncaught TypeError: Cannot read property 'ownerDocument' of undefined.
Make sure you're passing a selector to jQuery, not some form of data:
$( '.my-selector' )
not:
$( [ 'my-data' ] )
I had a similar issue.
I was using jQuery.map but I forgot to use jQuery.map(...).get() at the end to work with a normal array.
The same issue came up for me inside of $elms.each().
Because:
the function you pass to .each(Function) exposes (at least) two arguments; the first being the index and the second being the element in the current element in the list, and
because other similar looping methods give current the element in the array before the index
you may be tempted to do this:
$elms.each((item) => $(item).addClass('wrong'));
When this is what you need:
$elms.each((index, item) => $(item).addClass('wrong'));
In case you are appending to the DOM, make sure the content is compatible:
modal.find ('div.modal-body').append (content) // check content
If you use ES6 anon functions, it will conflict with $(this)
This works:
$('.dna-list').on('click', '.card', function(e) {
console.log($(this));
});
This doesn't work:
$('.dna-list').on('click', '.card', (e) => {
console.log($(this));
});
In my case, this error happened because my HTML had a trailing linebreak.
var myHtml = '<p>\
This should work.\
But does not.\
</p>\
';
jQuery('.something').append(myHtml); // this causes the error
To avoid the error, you just need to trim the HTML.
jQuery('.something').append(jQuery.trim(myHtml)); // this works
I am trying to write a rather simple "select all" feature, but I am getting errors with my javascript. The code is rather straight forward, so I'll just post it:
(function() {
$(function() {
var all_check_box;
all_check_box = '#tournament_league_127';
return $(all_check_box).change(function() {
return $('.leagueCheckBox').each(function() {
return this.prop("checked", true);
});
});
});
}).call(this);
This code was generated by the following CoffeeScript:
$ ->
all_check_box = '#tournament_league_127'
$(all_check_box).change ->
$('.leagueCheckBox').each ->
this.prop("checked", true)
However, when I click #tournament_league_127, I get the following error: this.prop is not a function. I'm not really sure what I'm doing wrong. Any help would be appreciated.
this refers to the element not the jQuery object so you need,
return $(this).prop("checked", true);
It should be $(this).prop ...(assuming jQuery 1.6+, before that .prop did not exist).
I am trying to check if an object with class sourceFocus has data in it. However when I check it, it does not have data when it should. What am I doing wrong here?
$('.source').click(function() {
$('.source').removeClass('sourceFocus');
$(this).addClass('sourceFocus');
$(this).data('source_selected', true);
console.log($.hasData(this));
console.log(this);
});
$('.target').click(function() {
$('.target').removeClass('targetFocus');
$(this).addClass('targetFocus');
$(this).data('target_used', true);
//$('.sourceFocus').data('source_used', true);
console.log($.hasData('.sourceFocus'));
if($.hasData('.sourceFocus')){
console.log("has data worked");
check_for_duplicates();
}
I don't think the .hasData() method accepts selectors in your case .sourceFocus, try selecting .sourcefocus as an element and then passing that to the .hasData() function.
try something like...
console.log($.hasData($('.sourceFocus:first')));
$.hasData() checks against a DOM Element
you have to get it out of the jQuery object, either using array notation or the .get() method (not to be confused with the $.get() ajax method)
console.log($.hasData($('.sourceFocus')[0]));
If you trying to read the HTML between the tags for which you are using .sourceFocus class then do this in your if statement:
$.hasData($('.sourceFocus').html())
I initiate the tinyMCE like this in multiple tabs of JQuery:Tab. But I find to init tinyMCE multiple times yields readonly text areas. Thus I wish to check if tinyMCE is already initated. Is there a method like isInitated() or something similarly convenient there?
tinyMCE.init({
mode : "textareas",
theme : "simple",
width : "500",
height : "300"
});
You can use tinymce.editors.length to see if there is already an editor instance initalized (tinymce.editors.length > 0).
I know this question is old, but...in case someone is still looking for the holy grail:
in tinymce 4, you can pass a callback to tinyMCE.init like so:
tinyMCE.init({
//your regular parameters here...
setup: function(editor) {
editor.on('init', function() {
//all your after init logics here.
});
}
});
You can add init_instance_callback to init() parameters. This callback will be invoked when the tinymce instance is already inited.
I am using tincyMCE 4.7.2
I tried the answer of #Thariama and it did not work for me, I guess because his answer is valid for the older versions of the tinyMCE.
here is what worked for me (again, according to the version you are working on, this could not be helpful for you)
if (tinymce.initialized === true)
To check if "tinyMCE" is set just use this:
if(typeof(tinyMCE) != "undefined") {}
Try this:
if (typeof(tinymce.activeEditor.contentDocument) !== "undefined") {
// initialized
}
I found other solution for this.
Let's say that You've got element
<textarea id="tinymce0"></textarea>
Now let's say that You initialize it:
let config = { selector : '#tinymce0'};
tinymce.init(config);
After that You can make this:
let tinmyMceInstance = tinymce.get('tinymce0');
if( tinmyMceInstance === null ){
// Your code if not initialized
}
Keep in mind:
this works only with id, seems like using classname for get() won't work
Works in:
{
releaseDate: "2019-05-09",
majorVersion: "5",
minorVersion: "0.5",
}
So it's probably: 5.5