72022

jQuery 1.3 - Issue with Setting an Attribute Value

Question:

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:

<ol><li>Why doesn't the first jQuery version work? </li> <li>Are .get(0).customAttribue = void(0); and .removeAttr("customAttribute); equivalent?</li> </ol>

Thanks.

Answer1:

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

Answer2:

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.

Answer3:

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 <strong>not work with custom attributes</strong> (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'

Answer4:

Uhmm, try this:

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

Recommend

  • Zend DB Select with multiple table joins
  • How to have an email link inside a text in Xaml?
  • F# slicing array by indices array
  • django - can't see the uploaded media files from apache
  • iOS, How to check whether the user likes a facebook page or not?
  • Rx produce and consume on different threads
  • Submission of new app with iAds
  • force json_encode to create strings
  • Converter from SAT to 3-SAT
  • Excel VBA How to populate a multi-dimensional (3d) array with values from multiple excel ranges?
  • I am receiving HibernateException “No Hibernate Session bound to thread, and configuration does not
  • How to load gif image while ajax content is loading and javascript [duplicate]
  • NHibernate manually control fetching
  • Sequential (transactional) API calls in angular 4 with state management
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • Using a canvas object in a thread to do simple animations - Java
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • HTML download movie download link
  • swift auto completion not working in Xcode6-Beta
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Javascript Callbacks with Object constructor
  • Deserializing XML into class C#
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Cannot Parse HTML Data Using Android / JSOUP
  • Comma separated Values
  • Matrix multiplication with MKL
  • How to include full .NET prerequisite for Wix Burn installer
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • C# - Getting references of reference
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Linking SubReports Without LinkChild/LinkMaster
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?