Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append a jQuery variable value inside the .html tag

I have a jquery variable which contains html tags to create checkboxes dynamically. Now as per my need i have to add this variable inside the .html tag of jquery to show the checkboxes inside the dialogue box ..Also my .html contains form tag to include the checkboxes code inside the form..

Here is my variabel which contains the html tags..

var chbx=<input type="checkbox"   id="Mumbai" name="Mumbai" value="Mumbai" />Mumbai<br />
<input type="checkbox"   id=" Delhi" name=" Delhi" value=" Delhi" /> Delhi<br />\
<input type="checkbox"   id=" Bangalore" name=" Bangalore" value=" Bangalore" /> Bangalore<br />

And here is my .html tag containing form ..

var $dialog = $('<div></div>').html("<form id='myform'>" +chbx+ "</form>")
        .dialog({
            autoOpen: false,
            title: 'Select Sites',
            buttons: {
                "Submit": function() {  $('form#myform').submit();},
                "Cancel": function() {$(this).dialog("close");}
            }
        });

i tried to append the variable like +chbx+ but its not happening and showing empty dialogue box..

like image 916
Adi Avatar asked Oct 30 '13 11:10

Adi


People also ask

How do you append a variable in HTML?

Use the <var> tag in HTML to add a variable. The HTML <var> tag is used to format text in a document. It can include a variable in a mathematical expression.

How do you append something in jQuery?

jQuery append() MethodThe append() method inserts specified content at the end of the selected elements. Tip: To insert content at the beginning of the selected elements, use the prepend() method.

How do you append a variable in JavaScript?

Use the addition (+) operator to concatenate a string with a variable, e.g. 'hello' + myVar . The addition (+) operator is used to concatenate strings or sum numbers.


1 Answers

HTML :

<div id="myDiv">
    <form id="myForm">
    </form> 
</div>

jQuery :

var chbx='<input type="checkbox" id="Mumbai" name="Mumbai" value="Mumbai" />Mumbai<br /> <input type="checkbox" id=" Delhi" name=" Delhi" value=" Delhi" /> Delhi<br/><input type="checkbox" id=" Bangalore" name=" Bangalore" value=" Bangalore"/>Bangalore<br />';

$("#myDiv form#myForm").html(chbx);

//to insert dynamically created form 
$("#myDiv").html("<form id='dynamicForm'>" +chbx + "'</form>");

Demo

like image 128
Md Ashaduzzaman Avatar answered Oct 14 '22 08:10

Md Ashaduzzaman