Reputation: 21
I am trying to have a li
element and a div
be "inline", but right now the div
text keeps being put right below the li
element. I'm using display: inline block
and jQuery as below:
$('#list').append(
'<div class="spanish">\
<li>'+new_task+'</li>\
<div>Test</div>\
</div>');
.spanish {
display:inline-block;
}
Upvotes: 0
Views: 296
Reputation: 500
As @ValeriySiestov mentioned, div
is a block element, and li
is a block element as well.
One way to fix your problem is to change the structure of the html you are appending, so it is a span
element within the li
element.
Note that list elements (i.e. ul
s or ol
s) can only have li
s as their children, but li
s can have anything as their children. Reference: Can I use a div inside a list item?
Assuming you have an unordered list with id #list, you can append a new list item to it like so:
var new_task = "walk the dog"
var li_string = `<li>${new_task}: <span>Test</span></li>`
$('#list').append(li_string);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="list">
<li>make bed</li>
<li>brush teeth</li>
</ul>
Note the variable new_task
is being interpolated within the string of html being appended to the list using template literals.
Template literals syntax:
string text ${expression} string text
Upvotes: 2
Reputation: 159
It seems from you code. You want to print div below li. There are few things to note:
Please check below answer if its helpful to you.
$( document ).ready(function() {
var new_task = "<div class='row'>row</div>";
$('#list').append(
'<div class="spanish">\
<ul>\
<li>'+new_task+'<div class="sub-row">Test</div></li>\
</ul>\
</div>');
});
.row {
display: inline;
clear:both:
widht:100%
}
.sub-row {
display:inline;
clear:both;
margin-left:2px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="list"></div>
Upvotes: 1
Reputation: 81
div - is a block (display: block) element by default. It stretches to 100% of parent's width li - is an item of the list (display: list-item) and also stretches to 100% of parent's width
If you want to change this behaviour, you need to add to div and li another display value, like this
.spanish div, .spanish li {
display: inline;
}
or
.spanish div, .spanish li {
display: inline-block;
}
In your code, you use
Upvotes: 1