1

This is my first stackoverflow question, so try to be nice. ;-D

My issue is this, I am refactoring some existing javascript code and using jQuery to do it. In several places I've come across javascript code similar to the following:

// some js code working with the customAttribute value
javascriptElementObject.customAttribue = void(0);

The javascriptElementObject is now a jQuery object and I have been attempting to use the following code to do the same thing:

// some js code working with the customAttribute value
javascriptElementObject.attr("customAttribute", void(0));

However, this does not seem to be doing anything. The following code works however:

javascriptElementObject.get(0).customAttribute = void(0);

I'm aware of jQuery's removeAttr() function, but have not used it so far because I don't know if it's equivalent to setting the attribute value to void(0).

So I guess that really means I have 2 questions:

  1. Why doesn't the first jQuery version work?
  2. Are .get(0).customAttribue = void(0); and .removeAttr("customAttribute); equivalent?

Thanks.

2
  • Is there a difference between customAttribue and customAttribute? or just a typo? Commented Jan 14, 2009 at 22:44
  • It was a typo, fixed it. Commented Sep 22, 2009 at 17:27

4 Answers 4

6

jQuery likes to overload its methods so:

obj.attr( name ) //retrieves the attribute value
obj.attr( name, value ) //sets the attribute

obj.attr( name, void(0) ) == obj.attr( name, null ) == obj.attr( name ) //i.e retrieving the attribute

You might want to try the following if you want to set an empty attribute

obj.attr( name, '' )

This will also apply to other methods jQuery.html() for example

Sign up to request clarification or add additional context in comments.

1 Comment

It's a little late but I've just noticed that attributes without values, such as .attr("selected") in an <option> tag actually return the value true if they are present (but I assume still undefined if not present). .attr( "selected", true) seemed to have the desirable result of adding the attribute back and in this case, making this option the one currently selected in the <select> drop down.
2

What are you trying to accomplish?

If the goal is to remove the value in the name/value pair, you might as well just remove the attribute entirely. I'm not aware of any intrinsic value in maintaining an attribute that has no value; in less standards-compliant browsers it may even cause a problem.

In general, the syntax of $(selector).attr(name, value) and $(selector).removeAttr(name) work very well (at least I've never seen it fail.)

If you're trying to use void(0) to keep A HREFs from firing you'd be better off using a "return false" as the click event on those A tags.

1 Comment

Part of the problem was that I was refactoring existing code, so I couldn't definitively determine the purpose of setting the attribute value to void(0). In debugging afterward, accessing the attribute value after it had been set to void(0) yielded an undefined result, so .removeAttr() should work.
2

The only way to work with custom attributes via jQuery objects is:

obj.get(0).myCustomAttr = 'some value';

That is because jQuery's attr() method will not work with custom attributes (except while applied on a XML-document).

Note also that meouw's answer regarding jQuery overloading functions is not precisely correct, because jQuery checks for the parameters passed to it in such a manner that:

jQuery.funcname(param)

and

jQuery.funcname(param, null)

differ, becacuse null !== undefined. For example:

var params_test = function(a) {
    if (a === undefined) {
        return 'called with no parameters';
    } else {
        return 'called with one parameter: ' + a;
    }
};
params_test(); // results in 'called with no parameters'
params_test(null); // results in 'called with one parameter: null'

Comments

0

Uhmm, try this:

javascriptElementObject.attr("customAttribute", void(0));
var _void = javascriptElementObject.attr("customAttribute");
alert(_void);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.