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

  • SEARCH
  • Home
  • 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 9202147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:11:12+00:00 2026-06-17T23:11:12+00:00

I have following html: <div class=copy_me_text> <div> <input type=text name=name /> <input type=hidden name=id

  • 0

I have following html:

<div class="copy_me_text">
    <div>
        <input type="text" name="name" />
        <input type="hidden" name="id" />
    </div>
</div>

<div class="copy_me_hidden">
    <div>
        <input type="hidden" name="name" />
        <input type="hidden" name="id" />
    </div>
</div>

And following js code:

var $cloned_text = $('.copy_me_text').clone();
$cloned_text.find('input[name="name"]').val("SOMETHING");
$cloned_text.find('input[name="id"]').val("SOMETHING");
console.log($cloned_text.html());

var $cloned_hidden = $('.copy_me_hidden').clone();
$cloned_hidden.find('input[name="name"]').val("SOMETHING");
$cloned_hidden.find('input[name="id"]').val("SOMETHING");
console.log($cloned_hidden.html());

And output is strange for me:

<div>
    <input name="name" type="text">
    <input value="SOMETHING" name="id" type="hidden">
</div>
<div>
    <input value="SOMETHING" name="name" type="hidden">
    <input value="SOMETHING" name="id" type="hidden">
</div>

I create also jsFiddle example.
Is it correct behavior? I don’t understand, why in .html() function, value of input type="text" is not returned.

  • 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-17T23:11:13+00:00Added an answer on June 17, 2026 at 11:11 pm

    This is not a strange jQuery behavior, its a strange DOM effect. jQuery.val() does nothing else than setting the value property of <input> element. By “property”, I mean the DOM property and not the node attribute – see .prop() vs .attr() for the difference.

    The .html() method, which returns the innerHTML serialisation of the DOM, is expected to show only attributes of the elements – their properties are irrelevant. This is the default behaviour, and when you want input values serialized you need to explicitly set them as attributes – $input.attr("value", $input.prop("value")).

    So why did simple val() work on the hidden input elements? The reason is the HTML specification. There are reflecting IDL attributes, where the DOM property is coupled with the node attribute, but the value attribute is none of those. Yet, the value IDL attribute has special modes, in which it reacts differently. To cite the spec:

    The attribute is in one of the following modes, which define its behavior:

    value

    On getting, it must return the current value of the element. On setting, it must set the element’s value to the new value, set the element’s dirty value [… and do a lot of other stuff].

    default

    On getting, if the element has a value attribute, it must return that attribute’s value; otherwise, it must return the empty string. On setting, it must set the element’s value attribute to the new value.

    [“default/on” and “filename” modes]

    Spot the difference? And now, let’s have a look at the states of the type attribute. And really, if we check the Bookkeeping details sections, we can notice that in the hidden state,

    The value IDL attribute applies to this element and is in mode default

    ‐ while in all other (textual) states the mode is “value”.


    TL;DR:

    (Only) On <input type="hidden"> elements, setting the value DOM property (input.value = …, $input.val(…), $input.prop("value", …)) also sets the value attribute and makes it serializable for innerHTML/.html().

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

Sidebar

Related Questions

I have the following html code: <div class=tests> <input name=premise id=premise type=radio class=required value=0><span>0</span>
I have the following html form <div> <p>Field1</p> <input type=text name=fld_one id=fld_one value= />
I have the following code: HTML: <div id=login class=transparent-div login_leaf_class> <input class=pointer login_fields_button id=submit_button
I have the following HTML code: <div class=dim> Menu <div class='hidden'>submenu1</div> <div class='hidden'>submenu2</div> </div>
Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
I have the following html <div class=someClass> Some text </div> <div class=someClass> Some other
I have the following HTML code: <section id=content class=grid_9> <div class=block-content grey-bg> <div class=container_12>
I have the following HTML code: <div id=summary_form> <textarea class=summary>Please fill in</textarea><br/> <textarea class=summary>Please
I have the following html code: <div class=result hide id=userNameTooShort><span class=error>Your username needs to
I have the following HTML structure: <div class=s1> <div class=s2> <span class=span1> <span>text</span> </span>

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.