Creating an element with custom HTML attributes in jQuery

I am trying to create an anchor with jQuery using some custom HTML5 attributes to get something like this.

$('<a/>', {href : "#local_anchor",text: "DUMMY_TOKEN", onClick:"remote_function('token')"}).attr("data-toggle", "modal") 

If I use this code, it works fine.

 $('<a/>', { href : "#local_anchor", text: "DUMMY_TOKEN", onClick:"remote_function('token')" }).attr("data-toggle", "modal") 

But I would like to pass the data as a parameter on the first href, text, etc. When I try to do this, I get a syntax error.

I also tried using .data (), but I could not set the value in the markup only in the DOM.

+4
source share
1 answer

Just specify data-toggle and it will work:

 $("<a/>", { href: "#local_anchor", text: "DUMMY_TOKEN", onClick: "remote_function('token')", "data-toggle": "modal" }); 
+10
source

All Articles