javascript - Create HTML element using Form Input value via jQuery -
i trying implement automatically html using form value.
when user enter information in form, js create correspond html him.
for example.
<div class="row" id="1"> <div class="form-group col-lg-2"> <select class="form-control" name="tag"> <option value="p" selected>p</option> <option value="br">br</option> </select> </div> <div class="form-group col-lg-2"> <select class="form-control" name="class"> <option value="day" selected>day</option> <option value="blocktime">blocktime</option> <option value="blocktitle">blocktitle</option> <option value="session">session</option> <option value="person">person</option> <option value="firm">firm</option> </select> </div> <div class="form-group col-lg-7"> <textarea class="form-control" rows="3" name="content"></textarea> </div> <div class="form-group col-lg-1"> <input type="button" class="btn btn-default" onclick="addline()" value="add"> </div> </div>
the user selected p, day, test message, , press "add" button, button call function addline()
. main part below, didn't make right.
var currenttag = currentline.find("select[name='tag'] option:selected").text(); var currentclass = currentline.find("select[name='class'] option:selected").text(); var currenttext = currentline.find("textarea").val(); var new_content = $("<currenttag></currenttag>").addclass(currentclass).html(currenttext);
now currenttag
value "p", currentclass
"day", currenttext
"test message", has been done.
this want, jquery creates html code this:
<p class="day">test message</p>
and want store html code variable called new_content
. ...
if have container these elements, append them it:
first want set new line variable:
var newelements = []; var = 0; //make index counter can use later if want to. optional /* selection script */ var htmlelement = "<" + currenttag + " class='" + currentclass + "'>" + currenttext + "</" + currenttag + ">"; newelement.push(htmlelement, i); $('.container').append(htmlelement); i++; //increment create index /* make sure of inside selection script. */
Comments
Post a Comment