3

I have to add dynamically the content of div using jquery.

enter image description here

when I choose 10 for example, I should see in the right side 10 option of : enter image description here

the code of these element are given as follow:

 <nav class="choose_option" id="navch">
            {{ form_widget(form.critere1) }}
            <select id="select1">
                <option value="normal" class="highlight_orange">Normal</option>
                <option value="nombre">Nombre</option>

            </select>
            <select id="op1">
                <option value="like" class="highlight_orange">Like</option>
                <option value="not like">NOT LIKE</option>
                <option value="=">=</option>
                <option value="<"><(inf)</option>
                <option value=">"><(sup)</option>
                <option value="<>"> <></option>
            </select>
            <input type="text" id="txtcrit1" {{ app.request.get('txtcrit1') }}/>
        </nav>
        <nav>
            <ul class="list">
                <li class="list__item left pushright">
                    <label class="label--checkbox">
                        <input type="radio" name="radio" value="AND" class="checkbox" id="chx1">
                        ET

                    </label>
                </li>
                <li class="list__item left pushright">
                    <label class="label--checkbox">
                        <input type="radio" name="radio" value="OR" class="checkbox" id="chx2">
                        OU

                    </label>
                </li>


            </ul>
        </nav>

my question, how I can add this same code dynamically using jquery and javascript in such a way when I choose five or tens option I get five or tens elements

4
  • You can add dynamic elements by using $(selector).html() or $(selector).append(). To add dynamic count, you can have loop to compute it. Commented Jan 15, 2016 at 10:51
  • What is {{ form_widget(form.critere1) }}? Not JS / CSS / HTML! Commented Jan 15, 2016 at 10:52
  • check for jquery template. This may solve your problem nit and clean. Commented Jan 15, 2016 at 10:53
  • {{ form_widget(form.critere1) }} is from symfony Commented Jan 15, 2016 at 10:54

2 Answers 2

2

I've done you a quick example of how you can do this:

$( document ).ready(function() 
{
     $('#selectBox').on('change', function() 
     {
       switch (this.value)
       {
          case "5":
          case "10":
           
           for (i = 0; i < this.value; i++) 
           { 
              $("#appendToMe").append('<nav class="choose_option" id="navch">{{ form_widget(form.critere1) }}<select id="select1"><option value="normal" class="highlight_orange">Normal</option><option value="nombre">Nombre</option></select><select id="op1"><option value="like" class="highlight_orange">Like</option><option value="not like">NOT LIKE</option><option value="=">=</option><option value="<"><(inf)</option><option value=">"><(sup)</option><option value="<>"> <></option></select><input type="text" id="txtcrit1" {{ app.request.get("txtcrit1") }}/></nav><nav><ul class="list"><li class="list__item left pushright"><label class="label--checkbox"><input type="radio" name="radio" value="AND" class="checkbox" id="chx1">ET</label></li><li class="list__item left pushright"><label class="label--checkbox"><input type="radio" name="radio" value="OR" class="checkbox" id="chx2">OU</label></li></ul></nav>');
           }
           
          break;
       }
     });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<select id="selectBox">
  <option>select option</option>
  <option>5</option>
  <option>10</option>
</select>

<div id="appendToMe">

</div>

Sign up to request clarification or add additional context in comments.

3 Comments

but me I want to append the full code that I mentionned in my question
@MajdiTaleb - No problem, Glad I could help you out :)
just still the problem of {{ form_widget(form.critere1) }} cannot be added, because this variable from symfony. thank you for this script
1
 use $(selector).append() to append data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.