Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8553411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:38:43+00:00 2026-06-11T14:38:43+00:00

Consider the following HTML: <select> <option value=hasAttr>Has a value attr</option> <option>Does not have a

  • 0

Consider the following HTML:

<select>
    <option value="hasAttr">Has a value attr</option>
    <option>Does not have a value attr</option>
</select>

As you can see, one of the options has a value attribute set and the other does not. However, when I go to check the val() for each of the options via jQuery, the option that does not have a value attribute set is returning the text inside of the tag (html()) instead of undefined as I would normally expect.

var $select = $('select');

$select.each(function() {
    var $this = $(this),
        $options = $this.children('option');

    $options.each(function(){
        var $option = $(this),
            $value  = $option.val(),
            $html   = $option.html();

        console.log('Option Value: '+ $value +'\nOption HTML: '+ $html);

    });

});

Even if I change the code to look for $option.attr('value'), I still get the same results. Is there a way that I can check to see if an <option> has a value attribute present that will return boolean?

Here is a jsFiddle.


Updated jsFiddle with solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:38:44+00:00Added an answer on June 11, 2026 at 2:38 pm

    This is correct, standard behaviour. In the absence of a value attribute, the textual content of the element is used as the element’s value.

    From the W3C website:

    If there isn’t [a value attribute], the value of an option element is the textContent of the element.


    You can test for the presence of the content attribute using getAttribute on the DOM node:

    $option[0].getAtribute('value') // returns null
    

    Alternatively, you can use the jQuery is method with the “has-attribute” selector:

    $option.is('[value]') // returns false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following HTML : <div class=list> <select> <option value=one>1</option> <option value=two>2</option> </select> And
Please consider the following HTML: <input type='text' id='name'/> <option id='option'> <select value='1'>hi</select> <select value='2'>bye</select>
Consider the following piece of code : <select> <option value=0>foo</option> <option value=1 selected=selected>bar</option> </select>
Consider the following HTML : <a id=add herf=#>add</a> <div class=list> <select> <option>1</option> <option>2</option> </select>
Consider the following example: ( live demo ) HTML: <select> <option>Hello</option> <option>Stack</option> <option class=a>Overflow</option>
Consider the following situation, I have a select element <select> <option>a</option> <option selected=selected>b</option> <option
Consider the following html http://www.carbide-red.com/prog/test_table.html I have worked out that I can move left
Consider the following HTML snippet. The desired effect is to have a dropdown be
Consider the following HTML snipped: <input type=submit value=Delete selected><input type=submit value=Print selected> <div class=table>
Consider an HTML form built in the following way: <select name=schoolType> @foreach (SchoolType schoolType

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.