Using HTML attributes in javascript -
i beginner in javascript. i'm trying add function generate new form elements using javascript, on page that's generated in php.
the code works in creating new <tr>, <td>, <input type="text">
html elements. when try create buttons using css styles, find styles lost tags.
if(document.createelement) var tr = document.createelement("tr"); var input = document.createelement("input"); // if ns passed, should become ns[2] etc input.id = field+"["+count+"]"; input.name = field+"["+count+"]"; input.type = "text"; //type of field - can valid input type text,file,checkbox etc. var td=document.createelement("td"); var newcontent = document.createtextnode("ns"); td.appendchild(newcontent); tr.appendchild(td); td=document.createelement("td"); td.appendchild(input); tr.appendchild(td); var btndel=document.createelement("a"); btndel.class="btn btn-primary"; btndel.onclick = "addfield(\'nameservers\',\'ns\',10);" ; var btntext=document.createelement("span"); btntext.class="btn-label"; btntext.innerhtml="add"; btndel.appendchild(btntext); td.appendchild(btndel); tr.appendchild(td); field_area.appendchild(tr); }
the produced code shows:
<a><span>add</span> </a> </td>
instead of expect:
<a onclick="addfield('nameservers','ns',10);" class="btn btn-primary"> <span class="btn-label">add </span> </a>
what doing wrong? what's proper way of passing html attributes using script?
for on click
instead of trying output html, why not in pure javascript, using addeventlistener
method?
element.addeventlistener('click', function() { addfield('nameservers','ns',10); }, false);
this approach known non-obtrusive javascript, , it's quite desirable attribute when developing website.
for class
as mentioned, use classname
, not class
.
class
precedes declaration of new class, , can't used attribute, in same way can't call variable var
.
Comments
Post a Comment