0

I am trying to make a dynamically button in javascript

    var button = document.createElement('input');
    button.setAttribute('type', 'submit');
    button.setAttribute('ID', 'btnSendMailClone');
    button.setAttribute('value', 'Submit');
    button.setAttribute('onclick', 'btnSendMail_Click()');
    button.setAttribute('form', 'myform');
    document.body.appendChild(button);
    button.setAttribute("class", "btn btn-primary");
    $('#button').addClass('myClass');
   $('#btnSendMailClone').css("margin-right", "100px")
    $('#btnSendMailClone').css("width", "98");

And by clicking on the event it should come on its click event

function btnSendMail_Click() {
           debugger;

        }

Button i debug its giving me error on this line

document.body.appendChild(button);

it says unable to get property append child.I am newbie to java script.Pleas guide what i am doing wrong

3
  • if you are using jquery, why can't you try $('#btnSendMailClone').click(function(){}); instead of writing a seperate function? Commented Jul 11, 2017 at 7:25
  • What browser are you using ? Commented Jul 11, 2017 at 7:26
  • Possible duplicate of Creating Dynamic button with click event in javascript Commented Jul 11, 2017 at 7:26

4 Answers 4

2

Your code is working fine

var button = document.createElement('input');
button.setAttribute('type', 'submit');
button.setAttribute('ID', 'btnSendMailClone');
button.setAttribute('value', 'Submit');
button.setAttribute('onclick', 'btnSendMail_Click()');
button.setAttribute('form', 'myform');
document.body.appendChild(button);
button.setAttribute("class", "btn btn-primary");
$('#button').addClass('myClass');
$('#btnSendMailClone').css("margin-right", "100px")
$('#btnSendMailClone').css("width", "98");

function btnSendMail_Click() {
  console.log("Submit !");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Possible problem A

Your are using IE then you will have to replace

document.body.appendChild(button);

with

document.getElementsByTagName('body')[0].appendChild(button);

Possible problem B

Your script is inside the <head>. This will execute the script before the <body> has been seen by the browser. To solve this, move your script at the end of the document

Or your can use $(document).ready(function(){...})

Sign up to request clarification or add additional context in comments.

Comments

2

I think you document not ready to append .Try with append button after document.ready .And don't forget to add jquery link

Updated

center position button

$(document).ready(function() {
  var button = document.createElement('input');
  button.setAttribute('type', 'submit');
  button.setAttribute('ID', 'btnSendMailClone');
  button.setAttribute('value', 'Submit');
  button.setAttribute('onclick', 'btnSendMail_Click()');
  button.setAttribute('form', 'myform');
  document.body.appendChild(button);
  button.setAttribute("class", "btn btn-primary");
  $('#button').addClass('myClass');
  $('#btnSendMailClone').addClass('center')

})

function btnSendMail_Click() {
  console.log($('input')[0].outerHTML)
}
.center {
  margin: auto;
  width: 98px;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

3 Comments

how can button come in center because here margin left right is not working
@janakgera You are using margin-right instead of margin-left
@janakgera margin not more effect with button center position try with some transfrom property exact center in horizontal.see my updated answer
0

You have got many options. One simple Solution would be to test if body exists using:

if(document.body != null){ document.body.appendChild(button); }

Another Option would be to use it by selecting the tag name:

document.getElementsByTagName('body')[0].appendChild(button);

For dynamic manipulation you should have a deep look on jQuery:

$('body').append(button);
or
$('body').html(button);

Comments

0

based on last line of your code it seems you are using jQuery, therefore I suggest you to create the button with it like :

var button = $('<button type="submit" id="btnSendMailClone" class="btn btn-primary" style="margin-right:100px:width:98%">Submit</button>');
button.on('click',btnSendMail_Click):
$('body').append(button);

hope it helps

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.