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 9185317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:14:56+00:00 2026-06-17T19:14:56+00:00

Possible Duplicate: Set attribute without value Sometimes, when handling with HTML, it is required

  • 0

Possible Duplicate:
Set attribute without value

Sometimes, when handling with HTML, it is required to set an attribute of a DOM element without using any value, e.g. see the selected parameter here:

<select id="auto-makers">
  ...
  <option value="fiat" selected>FIAT</option>
  <option value="ford">FORD Motor Company</option>
  <option value="gm">General Motors</option>
  ...
</select>

This kind of attributes in HTML documents are a not rare. For instance, here follows a list of possible attributes:

  • checked;
  • selected;
  • required;
  • multiple;
  • disabled;
  • etc.

How can I set a property with no value at runtime using Javascript and/or jQuery?

  • 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-17T19:14:57+00:00Added an answer on June 17, 2026 at 7:14 pm

    Following your example, suppose that you have

    <select id="auto-makers">
      ...
      <option value="fiat">FIAT</option>
      <option value="ford">FORD Motor Company</option>
      <option value="gm">General Motors</option>
      ...
    </select>
    

    and that you want to add the selected attribute to the <option> such that value="fiat". Hence, with pure Javascript you can use setAttribute method:

    var select = document.getElementsById('auto-makers');
    for (var i=0; i<select.options.length; i++){
       if (select.options[i].value=="fiat"){
         select.options[i].setAttribute('selected','');
         break;
       }
    }
    

    while with jQuery you can simply use the attr method:

    $('#auto-makers').find('option[value="fiat"]').attr('selected','');
    

    In both cases, an empty string is passed as value for the attribute.

    You can verify that the output is the one desired by looking at the related HTML code just after the changes you provided.

    Finally, notice that a similar question is: “Set attribute without value“.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: HTML5 form required attribute. Set custom validation message? in HTML 5 form
Possible Duplicate: How do I set an HTML class attribute in Markdown? In my
Possible Duplicate: HTML5 form required attribute. Set custom validation message? Simple_form has an automatic
Possible Duplicate: Sort element by numerical value of data attribute I want to reorganize
Possible Duplicate: Android: Set interval in SeekBar I have a seekbar with max value
Possible Duplicate: NSBackgroundColorAttributeName-like attribute in NSAttributedString on iOS? I am trying to set the
Possible Duplicate: jQuery .data() Not Updating DOM I am having a problem with using
Possible Duplicate: When to use setAttribute vs .attribute= in JavaScript? Why do you sometimes
Possible Duplicate: Can set any property of Python object In [67]: obj = object()
Possible Duplicate: JavaScript setAttribute vs .attribute= javascript dom, how to handle "special properties" as

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.