Note that there are some explanatory texts on larger screens.

plurals
  1. POdeclare html in jquery variable space error
    text
    copied!<p>i populate form using jquery based on dropdown i have one drop down to handle number of people and number of walker i generate my html in div based on no of people selected but i have so many fields and i have taken them in variable but when i use space in html it shows an error so what is the alternative to this how should i generate my html using variable in for loop</p> <pre><code>&lt;script src="http://code.jquery.com/jquery-1.9.1.js"&gt;&lt;/script&gt;&lt;/script&gt; &lt;script&gt; $(document).ready(function () { $("#num-people").change(function () { var countP = parseInt($("select#num-people option:selected").val()); $("#walker option").each(function (index) { if (countP &lt; index) { var newindex = index $("#walker option[value=" + newindex + "]").remove(); } }) for (var i = 1; i &lt;= countP; i++) { $("#walker option[value=" + i + "]").remove(); var j = i + 1; $('#walker').append(new Option('walker' + i, i)); var walk = "#walker"; var formhtml = "&lt;div id=walker" + i + " style='display:none'&gt;First Name:&lt;input type='text' name='wname" + i + "' id='wname" + i + "'&gt;Last Name:&lt;input type='text' name='lname' id='lname'&gt;&lt;/div&gt;"; //generate waler if not exist if ($(walk + i).length == 0) { $('#form_div').append(formhtml); } //display walker $("#walker").change(function () { var numwalker = parseInt($("select#walker option:selected").val()); $('#walker' + numwalker).css('display', 'block'); }) } }) }); &lt;/script&gt; &lt;select name="num-people" id="num-people"&gt; &lt;option name="1"&gt;1&lt;/option&gt; &lt;option name="2"&gt;2&lt;/option&gt; &lt;option name="3"&gt;3&lt;/option&gt; &lt;option name="4"&gt;4&lt;/option&gt; &lt;option name="5"&gt;5&lt;/option&gt; &lt;option name="6"&gt;6&lt;/option&gt; &lt;/select&gt; &lt;select name="walker" id="walker"&gt; &lt;option value=""&gt;Select Walker&lt;/option&gt; &lt;/select&gt; &lt;div id="form_div"&gt; &lt;/div&gt; </code></pre> <p>when i use html with space in variable it shows an error and i have many fields to mange then how to do this dynamically keeping varible value in div id etc..(using php code or using jquery but different way of generating html)</p> <pre><code>var formhtml= "&lt;div id=walker"+i+" style='display:none'&gt; First Name:&lt;input type='text' name='wname"+i+"' id='wname"+i+"'&gt; Last Name:&lt;input type='text' name='lname' id='lname'&gt; &lt;/div&gt;"; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload