Get object by attribute in IE8 - javascript

The following line works fine in IE9 but is giving me a 'Object doesn't support this property or method' error in IE8.
var id = $("td[title='Project Documents']");
Is there an IE8 compatible way to get an object based on a specific attribute?

According to the docs what you have should work. Try this:
$("td[title]").filter(function () {
return 'Project Documents' === $(this).attr('title');
});
It could be that the capitalization or whitespace chars do not match exactly.

I'd be tempted to test this on a title without any whitespace in the title to test to see if it is actually this causing the problem.

Related

substr in IE7 not working

I'm trying the following code to extract first few characters from a string stored in a variable.
title.substr(0,35)+"_"+var1+"_"+var2+var3;
At the above line IE7 throws an error:
Object doesn't support this property or method.
Tried: substring and slice as well. Still same issue.
It is quite apparent from the error message that title is not of type string.
Try this:
var title = "abcdefghijklmnopqrstuvwxyz";
console.log(title.substr(0,15));
I suspect your title variable is not of type String. To debug it, try
console.log(typeof(title)) in your own code.
Check the console and see what you get.

Why doesn't IE8 like this JS?

IE8 has been throwing this error at me
SCRIPT65535: Unexpected call to method or property access.
load-scripts.php, line 4 character 25690
I removed a .js file from the code, and the error went away. I started commenting functions out, and narrowed it down to this one. With this one commented, I don't get the error. With it active, I do get it
$("title, .ab-item").each(function() {
var text = $(this).text();
text = text.replace("RepAgent", "Review Scout");
$(this).text(text);
});
I've used JSHint and it says that it's valid?
I'm pretty sure that Internet Explorer doesn't like you messing with <title> element contents. That's not really how you set the document title anyway; just set document.title.
jQuery uses appendChild inside $.text() .
Although <title/> has a appendChild-method(inherited from HTMLElement), this method may not be used.(it's also not listed in the title-methods)

'target.html()' is null or not an object

I'm trying to replace all occurrences of a certain character (quotes) from an element. My code works fine in Chrome and FF but fails in IE with the debugger saying - 'target.html()' is null or not an object
here is what my code looks like -
text = "some random text";
target = $('#target');
target.append(text);
target.html(target.html().replace(/"/g, " "));
What's causing that error in IE and how do i fix it?
'target' is used an attribute and IE does not like it if you use it as a variable name. In fact it even refuses to recognize event.target and insists on event.srcElement (tell me about it ..) .
Anyways, it should work if you rename the object to $target.
One of the main reasons I get such errors is because the HTML is not correctly formed(and it sure gave me hell before I found out). Other browsers allow a missing '>' or other syntactical errors, but IE is very strict.
So just see minutely that the markup in target.html() is correct.

IE7 converting bracket notation to . notation in javascript

I have this line of code (where 'e' is a click event):
var type = $(e.currentTarget.parentNode)[0].classList[0];
which is producing this error in IE7 (using Companion.JS to report errors):
'0.classList.0' is null or not an object
I tried the following variations on my code, but get the same result:
var type = $(e.currentTarget).parent()[0].classList[0];
var type = $(e.currentTarget).parent()['0'].classList['0'];
This code works in the latest Chrome and Firefox browsers. Any idea what's going on here?
First check the .length of $(e.currentTarget.parentNode), you might have to add a condition for IE because the currentTarget is inconsistent with other browsers.
Also, classList is not supported in IE.
Code with classList does not work in IE?
parse the .attr('class') or [0].className

is this a bug? please check it out

html
<div contentEditable="true">testing....</div>
jQuery
$(document).ready(function(){
$('[contenteditable]').removeAttr('contenteditable');
});
above codes is fine and working. you can feel it here.
Now, try this
$('[contentEditable]').removeAttr('contentEditable');
// notice the CamelCase of the string contentEditable
in FF 3.6, it gives an error on the console
An invalid or illegal string was
specified" code: "12 elem[ name ]
= value;
and the div is still editable.
I suspected it was the jQuery selector, but is not. By further inspection, it was the argument passed on the .removeAttr('contentEditable');. It works when all small letters. So, I thought it should be all small letters. I'm curious so I tried adding CLass as an attribute and do .removeAttr('CLass');. But then it works without error.
So, how come contentEditable is giving me that error?
update
from Kobi, it seems that it actually accept any case except, contentEditable (I did try too).
CamelCase
This isn't about small letters, but about the exact casing. Any other casing than contentEditable works, for example: removeAttr('ConTentEditable');.
I can't find the exact source of the problem, I guess it's a Firefox restriction.
It seems jQuery sets the attribute to an empty string before removing it, which is what's causing the error. This seems to work better:
$('[contentEditable]').attr('contentEditable', false);
You could call it a bug, but really the framework is designed this way. removeAttr, along with other attr functions, points to jQuery.attr() to set the attribute's value. After setting the attribute to "", it then attempts to remove it. The code for attr() specifically checks to see if the given string is a property name on the object first using the in operator:
// If applicable, access the attribute via the DOM 0 way
if ( name in elem && notxml && !special ) {
(from jQuery 1.4, line 1452-1453)
Since you're supplying the camelCase property name, it uses that instead of elem.setAttribute(), which is specifically the cause of the problem. For any other case, name in elem would return false (because property names are case sensitive), which is why it's successful then. jQuery does this mostly to work around cross browser issues with setAttribute().
It looks like Firefox has a problem with setting the property to an empty string, unless you have the same problem in other browsers. You could try and file a bug either on the jQuery site or MDC.
contentEditable seams to be a special attribute:
http://www.whatwg.org/specs/web-apps/current-work/multipage/editing.html#contenteditable
The contentEditable property (not attribute, since that isn't what attr() and friends usually deal with) expects a string value, one of "true", "false" and "inherit". I wouldn't use jQuery to turn off contentEditable, but I imagine the following would work:
$('[contenteditable]').attr("contentEditable", "false");
Or you could bypass jQuery for setting the actual contentEditable property:
$('[contenteditable]').each(function() {
this.contentEditable = "false";
});

Categories