Reputation: 43
I have this code for showing data from MYSQL. I can fill the divs with innerHTML, but when I use += then it concates all the values in the array. But i need to create new elements in div. How should i implement it?
jQuery(document).ready( function() {
$(".dropdown-item").click( function() {
var data = { action: 'get_data',
id: $(this).attr('id')
};
var parent= document.getElementById('info-block bg-grey m-b-2 p-a');
var name = document.getElementsByClassName('h4 underline-sm')[0];
var job = document.getElementsByClassName('m-b-1')[3];
var phone=document.getElementsByClassName('m-b-0')[6];
var image= document.getElementsByClassName('col-sm-4 text-xs-right m-b-1')[0];
jQuery.ajax({
cache: false,
url: '/wp-admin/admin-ajax.php',
data: data,
dataType:'JSON',
method: 'POST',
success: function(response) {
$.each(response, function(key, value){
//
name.innerHTML=response[key].firstname + ' ' + response[key].lastname;
job.innerHTML ='<b>' + $.trim(response[key].job_title) + ' <br>Tööruum: ' + $.trim(response[key].room) + '<br/><a href="mailto:' + $.trim(response[key].email) + '>"' + $.trim(response[key].email)+ '</a></b>' ;
phone.innerHTML=$.trim(response[key].phone) + ' ' + $.trim(response[key].cell_phone);
image.innerHTML='<img src="'+ $.trim(response[key].image) +'" class="rounded-circle lg" alt="">';
});
}
});
});
});
I'm adding a div from HTML also to show what div i need to fill and create with loop.
<div class="info-block bg-grey m-b-2 p-a-2"> <!--parent div-->
<h4 class="h4 underline-sm"> </h4> <!--response[key].firstname+ ' ' response[key].lastname-->
<div class="col-sm-8 text-xs-left">
<p class="m-b-1"></p> <!--'<b>' + $.trim(response[key].job_title) + ' <br>Tööruum: ' + $.trim(response[key].room) + '<br/><a href="mailto:' + $.trim(response[key].email) + '>"' + $.trim(response[key].email)+ '</a></b>' -->
<p class="m-b-0"></p> <!--response[key].phone + ' ' + response[key].cell_phone-->
</div>
<div class=" col-sm-4 text-xs-right m-b-1">
<img src="" class="rounded-circle lg" alt=""> <!--to src response[key].image-->
</div>
</div>
I don't need someone to do the work, but someone maby could explain how to use createelement, and how to create child-divs to parent div with DOM elements.
Thank you all for your time you spend to think on my problem.
Upvotes: 3
Views: 2265
Reputation: 1200
Just a sample first create a wrapper div with id wrapper and create your required html in variable and append in the wrapper. for example
<div id="wrapper"></div> //this is your wrapper which holds all the html you want to insert
then in js
var html = `<div>
<h2>`
+ $.trim(response[key].job_title) +
`</h2>`
`<p class="`+response[key].firstname+ `">`
+ $.trim(response[key].phone) +
`<p>
</div>`
$("#wrapper").append(html);
it will append the required html inside wrapper. every new html will be appended inside wrapper but below the previous html. hope this gives you some idea
Upvotes: 1
Reputation: 157
You could use jQuery append function to do that, here is a quick example:
$('.h4').append(response[key].firstname + ' ' + response[key].lastname);
OR
$('.underline-sm').append(response[key].firstname + ' ' + response[key].lastname);
Upvotes: 2
Reputation: 42054
I'd suggest to .clone() your div each time you need to create a new one with specific values:
$.each(response, function(key, value){
var clonedEle = $('.info-block.bg-grey.m-b-2.p-a-2').clone();
clonedEle.find('.h4.underline-sm').text(value.firstname+ ' ' + value.lastname);
...........
Your final code will be:
jQuery(document).ready( function() {
$(".dropdown-item").click(function() {
var data = { action: 'get_data',
id: $(this).attr('id')
};
jQuery.ajax({
cache: false,
url: '/wp-admin/admin-ajax.php',
data: data,
dataType:'JSON',
method: 'POST',
success: function(response) {
$.each(response, function(key, value){
var clonedEle = $('.info-block.bg-grey.m-b-2.p-a-2').clone();
clonedEle.find('.h4.underline-sm').text(value.firstname+ ' ' + value.lastname);
clonedEle.find('.m-b-1').append('<b>' + $.trim(value.job_title) + ' <br>Tööruum: ' +
$.trim(value.room) + '<br/><a href="mailto:' + $.trim(value.email) + '>"' + $.trim(value.email)+ '</a></b>');
clonedEle.find('.m-b-0').text(value.phone + ' ' + value.cell_phone);
clonedEle.find('img.rounded-circle.lg').attr('src', value.image);
clonedEle.insertAfter('.info-block.bg-grey.m-b-2.p-a-2:first');
});
}
})
})
});
var response = [{firstname: 'firstname1', job_title: 'job_title', room: 'room', email: 'email', phone: 'phone', cell_phone: 'cell_phone', image: 'image'},
{firstname: 'firstname2', job_title: 'job_title', room: 'room', email: 'email', phone: 'phone', cell_phone: 'cell_phone', image: 'image'}]
$.each(response, function(key, value){
var clonedEle = $('.info-block.bg-grey.m-b-2.p-a-2').clone();
clonedEle.find('.h4.underline-sm').text(value.firstname+ ' ' + value.lastname);
clonedEle.find('.m-b-1').append('<b>' + $.trim(value.job_title) + ' <br>Tööruum: ' +
$.trim(value.room) + '<br/><a href="mailto:' + $.trim(value.email) + '>"' + $.trim(value.email)+ '</a></b>');
clonedEle.find('.m-b-0').text(value.phone + ' ' + value.cell_phone);
clonedEle.find('img.rounded-circle.lg').attr('src', value.image);
clonedEle.insertAfter('.info-block.bg-grey.m-b-2.p-a-2:first');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="info-block bg-grey m-b-2 p-a-2"> <!--parent div-->
<h4 class="h4 underline-sm"> </h4> <!--response[key].firstname+ ' ' response[key].lastname-->
<div class="col-sm-8 text-xs-left">
<p class="m-b-1"></p> <!--'<b>' + $.trim(response[key].job_title) + ' <br>Tööruum: ' + $.trim(response[key].room) + '<br/><a href="mailto:' + $.trim(response[key].email) + '>"' + $.trim(response[key].email)+ '</a></b>' -->
<p class="m-b-0"></p> <!--response[key].phone + ' ' + response[key].cell_phone-->
</div>
<div class=" col-sm-4 text-xs-right m-b-1">
<img src="" class="rounded-circle lg" alt=""> <!--to src response[key].image-->
</div>
</div>
Upvotes: 0