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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:53:22+00:00 2026-06-02T03:53:22+00:00

I’m trying to understand HTML elements that are dynamically created with JavaScript. Specifically can

  • 0

I’m trying to understand HTML elements that are dynamically created with JavaScript. Specifically can someone explain why if I create a button in HTML with no value attribute it will appear with no text. I can then add a value to it and the text will appear in the button, and I can also alert the value like so:

var Button1 = document.getElementById("Button1");
Button1.value = "test";
alert(Button1.value);

BUT, if I generate the button dynamically and then add the value it does NOT appear on the button, although I can still alert it.

var Button1 = document.createElement("button");
var Div1 = document.getElementById("Div1");
Button1.value = "test";
Div1.appendChild(Button1);
alert(Button1.value);

In the second example, the text of the button doens’t change, but the value still alerts. I KNOW that I can use createTextNode and append it to the button, but I’m trying to understand the difference between controls generated dynamically through JavaScript and those that are created in HTML.

Thank you for reading my question.

  • 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-02T03:53:24+00:00Added an answer on June 2, 2026 at 3:53 am

    In your second example you’re creating a new element and assigning a value to it, but you haven’t added it to the DOM — it exists purely in JavaScript which is why the alert works. In order to see the button on the page, you would need to append it to the document, typically with appendChild().

    You said, “BUT, if I generate the button dynamically and then add the value it does NOT appear on the button, although I can still alert it.” and “the text of the button doens’t change, but the value still alerts” which confuses me a little since it seems as if you might have a button already on the page which you expect the second example’s code to change.

    Your first example is a textbook example of how to get an element that exists in the DOM already, assign (or change) its value, and then alert it.

    Update: To answer your revised question, your code is changing the button’s value, but if you expect to see the text of the button on the page change, you need to alter its innerHTML value, like with Button1.innerHTML = "test";. Button elements aren’t like input buttons where the value is shown as the text of the button. The text of a button element comes from the text between the opening <button> and closing </button> tags.

    Example: <button value="test">test</button>

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

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.