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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:54:50+00:00 2026-05-26T01:54:50+00:00

How can I create a dynamic button with a click event with JavaScript? I

  • 0

How can I create a dynamic button with a click event with JavaScript?

I tried this, but when I click the add button, an alert message show up! It’s not what I want – I want to be able to click the button which is dynamically created.

<script language="javascript">
    function add(type) {
        //Create an input type dynamically.   
        var element = document.createElement("input");
        //Assign different attributes to the element. 
        element.setAttribute("type", type);
        element.setAttribute("value", type);
        element.setAttribute("name", type);
        element.setAttribute("onclick", alert("blabla"));

        var foo = document.getElementById("fooBar");
        //Append the element in page (in span).  
        foo.appendChild(element);

    }
</script>
  • 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-05-26T01:54:51+00:00Added an answer on May 26, 2026 at 1:54 am

    Wow you’re close. Edits in comments:

    function add(type) {
      //Create an input type dynamically.   
      var element = document.createElement("input");
      //Assign different attributes to the element. 
      element.type = type;
      element.value = type; // Really? You want the default value to be the type string?
      element.name = type; // And the name too?
      element.onclick = function() { // Note this is a function
        alert("blabla");
      };
    
      var foo = document.getElementById("fooBar");
      //Append the element in page (in span).  
      foo.appendChild(element);
    }
    document.getElementById("btnAdd").onclick = function() {
      add("text");
    };
    <input type="button" id="btnAdd" value="Add Text Field">
    <p id="fooBar">Fields:</p>

    Now, instead of setting the onclick property of the element, which is called “DOM0 event handling,” you might consider using addEventListener (on most browsers) or attachEvent (on all but very recent Microsoft browsers) — you’ll have to detect and handle both cases — as that form, called “DOM2 event handling,” has more flexibility. But if you don’t need multiple handlers and such, the old DOM0 way works fine.


    Separately from the above: You might consider using a good JavaScript library like jQuery, Prototype, YUI, Closure, or any of several others. They smooth over browsers differences like the addEventListener / attachEvent thing, provide useful utility features, and various other things. Obviously there’s nothing a library can do that you can’t do without one, as the libraries are just JavaScript code. But when you use a good library with a broad user base, you get the benefit of a huge amount of work already done by other people dealing with those browsers differences, etc.

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

Sidebar

Related Questions

In Postgresql you can create additional Aggregate Functions with CREATE AGGREGATE name(...); But this
In this page there's a way to dynamic add html (textbox,input button and radio
this question can create a misunderstanding: I know I have to use CSS to
Is there anyway I can create a not in clause like I would have
Well, I tried reading as much as I could but can't seem to find
I can create a menu item in the Windows Explorer context menu by adding
I can create the following and reference it using area[0].states[0] area[0].cities[0] var area =
I can create a literal long by appending an L to the value; why
I want to know if i can create a custom google maps application,on which
Is there a website I can create an HTML final four bracket? Or is

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.