Home > Enterprise >  jQuery create an element with attributes which contain hyphen Unexpected token '-'
jQuery create an element with attributes which contain hyphen Unexpected token '-'

Time:10-07

I am trying to create a button with bootstrap classes like below.

$("<button/>",
  {
  type: 'button',
  class:'close',
  data-dismiss: 'alert',
  aria-hidden:'true'}).appendTo("#testt");

Since data-dismiss has hyphen, it shows Unexpected token '-'.

How to fix this?

Thanks!

CodePudding user response:

You can write data-dismiss as a string, it will not be a problem

like:

$("<button/>", {
  type: 'button',
  class: 'close',
  'data-dismiss': 'alert',
  'aria-hidden': 'true'
}).appendTo("#testt");

CodePudding user response:

Change your code to this.

$("<button/>", {
    type: "button",
    class: "close",
    dataDismiss: "alert",
    ariaHidden: "true",
}).appendTo("#testt");
  • Related