2

I have the following html markup inside a Div named item and I want to select all the elements (inside nested divs) and clear the values. As shown in following given Jquery I have managed to access elements in each Div by using.children().each(). But the the problem is .children().each()goes one level down at a time from the parent div, so I have repeated the same code block with multiple .children() to access the elements inside nested Divs, can anyone suggest me a method to do this without repeating the code for N number of nested divs .

html markup

<div class="item">
    <input type="hidden" value="1234" name="testVal">

    <div class="form-group" id="c1">
        <div class="controls ">
            <input type="text" value="Results" name="s1" maxlength="255" id="id2">
        </div>
    </div>
    <div class="form-group" id="id4">
        <input type="text" value="Results" name="s12" maxlength="255" id="id225">

        <div class="form-group" id="id41">
            <input type="text" value="Results" name="s12" maxlength="255" id="5">

            <div class="form-group" id="id42">
                <input type="text" value="Results" name="s12" maxlength="255" id="5">

                <div class="form-group" id="id43">
                    <input type="text" value="Results" name="s12" maxlength="255" id="id224">
                </div>
            </div>
        </div>
    </div>
</div>

My Qjuery script

var row = $(".item:first").clone(false).get(0);
$(row).children().each(function () {
updateElementIndex(this, prefix, formCount);

if ($(this).attr('type') == 'text') {
    $(this).val('');
}
if ($(this).attr('type') == 'hidden' && ($(this).attr('name') != 'csrfmiddlewaretoken')) {
    $(this).val('');
}
if ($(this).attr('type') == 'file') {
    $(this).val('');
}
if ($(this).attr('type') == 'checkbox') {
    $(this).attr('checked', false);
}
$(this).remove('a');
});

// Relabel or rename all the relevant bits
$(row).children().children().each(function () {
updateElementIndex(this, prefix, formCount)

if ($(this).attr('type') == 'text') {
    $(this).val('');
}
if ($(this).attr('type') == 'hidden' && ($(this).attr('name') != 'csrfmiddlewaretoken')) {
    $(this).val('');
}
if ($(this).attr('type') == 'file') {
    $(this).val('');
}
if ($(this).attr('type') == 'checkbox') {
    $(this).attr('checked', false);
}
$(this).remove('a');
});

5 Answers 5

1

Seems something like this would do the trick. You of course be more specific about which item you select.

$('.item').find('input').val('');
Sign up to request clarification or add additional context in comments.

Comments

0

Try

var row = $(".item:first").clone(false).get(0);
$(row).find('input:not(:checkbox)').val('');
$(row).find('input:checkbox').prop('checked', false);

Comments

0

You can use find instead of children and it will traverse all the way down the dom.

$(row).find('input').each(function () {

Comments

0

Try this

 var row = $(".item:first").clone(false).get(0);
     $(row).children().each(function () {
         var $this=$(this);
         clearelements($this);
         if(element.children().length > 0)
          {
           element.children().each(function(){
              recursive(this)
            clearelements(this);
           });
         }
     });


    function recursive(element)
     {

    if(element.children().length > 0)
     {
      element.children().each(function(){
      clearelements(this);
      });
     }
 }

      function clearelements(obj){
         if ($(obj).attr('type') == 'text') {
            $(obj).val('');
          }
         if ($(obj).attr('type') == 'hidden' && ($(this).attr('name') != 'csrfmiddlewaretoken'))   {
           $(obj).val('');
           }
           if ($(obj).attr('type') == 'file') {
           $(obj).val('');
            }
           if ($(obj).attr('type') == 'checkbox') {
           $(obj).attr('checked', false);
            }
           $(obj).remove('a');
         }

or simply you can access them all at a time

    $(".item :input:not([name=csrfmiddlewaretoken]).val('');
    $(".item :checkbox).prop('checked', false);
    $(".item a").remove();

1 Comment

hi @SoftwareNerd thanks for your solution. you have used a function named recursive but you haven't called that function and you have used if(element.children().length > 0) in your 5th line of the given code and i wonder where it came form. its nice if you can give me a explanation
0
$(".item :input:not([name=csrfmiddlewaretoken])").val('');
$(".item :checkbox").prop("checked", false);
$(".item a").remove();

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.