2

i met a problem when tried to put some data in my block. I have div structure like this:

<div class="1">
    <div class="2">
        <div class="3">
            <div class="4"></div>
         </div>
         <div class="5"></div>
    </div>
</div>
 <div class="1">
    <div class="2">
        <div class="3">
            <div class="4"></div>
         </div>
         <div class="5"></div>
    </div>
</div>

...

and i need to place some data only in<div class="4"></div>

when i use this JS:

function sendDataChild(btn) {
var form = $(btn).closest('FORM[name=answer-form]');
var data = form.serialize();
$.ajax({
    type: "POST",
    url: url,
    dataType: "json",
    data: data,
    cache: false,
    success: function (data) {
        form[0].reset();
        $("div.4").html(data.error_name);
    },
    error: function (xhr, str) {
        alert('Возникла ошибка: ' + xhr.responseCode);
    }
});
return false;
};

it puts data in all div's with class = "4"

I tried to find parent, but it just put data in parent:

$("div.4").parent().html(data.error_name);

Can U advise how to make it right? P.S I used numbers just for example.)

myrko
  • 49
  • 1
  • 9

2 Answers2

2

As long as your button is under one div.1 you can replace your

$("div.4").html(data.error_name);

with

$(btn).closest("div.1").find("div.4").html(data.error_name);

Edit: Like the comments say you should not start your class names with numbers, but I hope this are just placeholders.

Raidri
  • 17,258
  • 9
  • 62
  • 65
0

I suggest you identify your div by id and use the append method.

stevemarvell
  • 981
  • 1
  • 6
  • 16