Results 1 to 4 of 4

Thread: attr() won't work on JQuery object

  1. #1
    Join Date
    Jun 2008
    Posts
    107
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default attr() won't work on JQuery object

    Hello,

    I'm retrieving an element on the page with JQuery and trying to get the "value" attribute like this:

    var myVar = $("#elmId").attr("value");

    but myVar ends up being "undefined".

    I know I can get the element itself because this line works fine:

    var myVar = $("#elmId");

    Why doesn't attr() work?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,925
    Thanks
    43
    Thanked 3,187 Times in 3,151 Posts
    Blog Entries
    12

    Default

    Hard to say for sure without fiddling around a bit and/or seeing your page.

    But I can tell you that's not how to get the value, and that technically speaking only form elements may have attribute values (they don't have to be in forms though), and that technically it's a property, not an attribute. It's both really, except that once the page is loaded, the attribute should remain the same regardless of user interaction and/or javascript effects, the property can be changed.

    That all said, if what you have is a form element (input, select, textarea, radio button, checkbox), and it does have some value, here's how to get its value in jQuery:

    Code:
    var myVar = $('#elmid').val();
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2008
    Posts
    107
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Hard to say for sure without fiddling around a bit and/or seeing your page.

    But I can tell you that's not how to get the value, and that technically speaking only form elements may have attribute values (they don't have to be in forms though), and that technically it's a property, not an attribute. It's both really, except that once the page is loaded, the attribute should remain the same regardless of user interaction and/or javascript effects, the property can be changed.

    That all said, if what you have is a form element (input, select, textarea, radio button, checkbox), and it does have some value, here's how to get its value in jQuery:

    Code:
    var myVar = $('#elmid').val();
    Thanks, but the problem I was having (which prevented any method from working) was that the script was being run before the DOM objects were available. The script was at the top of the page and it called my function (which included the code in the OP) before anything was loaded onto the page. Replacing the function call with:

    $(document).ready(MyFunction);

    did the trick. It says to call MyFunction() only when the page is ready.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,925
    Thanks
    43
    Thanked 3,187 Times in 3,151 Posts
    Blog Entries
    12

    Default

    Right, that would fall under the category of:

    if what you have is a form element
    You didn't. And under:

    Hard to say for sure without fiddling around a bit and/or seeing your page.
    But what I said about getting an element's value is still true. If you go for the attribute (.getAttribute('value') in javascript, .attr('value') in jQuery) you may come up with unexpected results in some browsers. In jQuery use .val(), in javascript use .value.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Replies: 2
    Last Post: 12-12-2012, 08:54 AM
  2. jquery rotate don't work
    By davelf in forum JavaScript
    Replies: 4
    Last Post: 02-10-2012, 05:20 AM
  3. "undefined" is null or is an object in jquery.min.js (error only in IE 8)
    By dsanchez in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 11-09-2010, 03:39 AM
  4. Replies: 4
    Last Post: 10-06-2009, 01:01 AM
  5. Replies: 2
    Last Post: 08-20-2005, 04:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •