Click here to Skip to main content
15,886,072 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
can one help me how to define my html code to a jquery variable
i like to use this html code to jquery code

XML
<div class="col-md-6" id="newtextbox">
                                    <label>HTS Code:</label>
                                    <button type="button" value="add" class="add" id="add"> <i class="icon-plus-circle2"></i></button>
          <div class="input-group">
           <span class="input-group-addon">
            <input type="checkbox" class="styled" name="input-addon-checkbox" >
           </span>
           <input type="text" class="form-control" placeholder="35702.03.012">
          </div>
         </div>



i have sample code we can see using button id he is able to create dynamically a check box and buton but i am not able to get how to add my html code to a variable. i just posted my html div code above how to assign entire div to a variable...?



XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Show Popup on Page Load</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<style type="text/css">
body
{
    font-family:Gill Sans MT;
    padding:10px;
}
fieldset
{
    border: solid 1px #000;
    padding:10px;
    display:block;
    clear:both;
    margin:5px 0px;
}
legend
{
    padding:0px 10px;
    background:black;
    color:#FFF;
}
input.add
{
    float:right;
}
input.fieldname
{
    float:left;

    display:block;
    margin:5px;
}
input.fieldcheck
{
    float:left;
    display:block;
    clear:left;
    margin:5px;
    margin-top:10px;
    }
select.fieldtype
{
    float:left;
    display:block;
    margin:5px;
}
input.remove
{
    float:left;
    display:block;
    margin:5px;
}
#yourform label
{
    float:left;
    clear:left;
    display:block;
    margin:5px;
}
#yourform input, #yourform textarea
{
    float:left;
    display:block;
    margin:5px;
}
</style>
<script type='text/javascript'>
$(document).ready(function() {
    $("#add").click(function() {
        var intId = $("#buildyourform div").length + 1;
        var fieldWrapper = $("<div class=\"fieldwrapper\" id=\"field" + intId + "\"/>");
        var fchk = $("<input type=\"checkbox\" class=\"fieldcheck\" /> ")
        var fName = $("<input type=\"text\" class=\"fieldname\" />");
        var removeButton = $("<input type=\"button\" class=\"remove\" value=\"-\" />");
        removeButton.click(function() {
            $(this).parent().remove();
        });
        fieldWrapper.append(fchk);
        fieldWrapper.append(fName);
        fieldWrapper.append(removeButton);
        $("#buildyourform").append(fieldWrapper);
    });

});
</script>
</head>
<body>
<input type="button" value="Add a field" class="add" id="add" />
</body>
</html>
Posted
Updated 11-Aug-14 1:15am
v2

The parent div is missing!

Simply add a parent div:
HTML
<div id="buildyourform"></div>


within your body tag.
 
Share this answer
 
Thanks for your Valuable solution now its working.
i am facing problem while adding an icon in my textbox can any one help out with this.

<script type="text/javascript">
$(document).ready(function() {
$("#add").click(function() {
var intId = $("#buildyourform div").length + 1;
var fieldWrapper = $("");
var fchk = $("<input type="\"checkbox\"" class="\"fieldcheck\"" /> ")
var fName = $("<input type=\"text\" class=\"form-control\" placeholder=\"Username\" />
var removeButton = $("<input type="\"button\"" class="\"remove\"" value="\"-\"" />");
removeButton.click(function() {
$(this).parent().remove();
});
fieldWrapper.append(fchk);
fieldWrapper.append(fName);
fieldWrapper.append(removeButton);
$("#buildyourform").append(fieldWrapper);
});

});

but the icon displays outside the text box can any one help with this</script>
 
Share this answer
 
v2
Comments
François Wahl 14-Aug-14 3:53am    
You should probably post any new questions as separate questions instead of posting a new question as a solution.
Trung Nguyen Son 14-Aug-14 4:03am    
Post a new question man!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900