0

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;
}
4
  • 1
    please provide more of your code, including the rest of the HTML and CSS markup. Commented Sep 22, 2018 at 19:25
  • 1
    The only allowed parent elements for li are ul and ol. You are creating invalid HTML. You cannot have an li as a child of a div. Commented Sep 23, 2018 at 9:36
  • Following up on @connexo's comment: you cannot have an li as a child of a div, but you can have a div as a child of an li. Commented Sep 23, 2018 at 20:35
  • So instead of appending something like <div><li>${new_task}</li> <div>Test</div></div>, you could append something like <li>${new_task} <span>Test</span></li>. In this case, a span may be better than a div for your purposes, since a span is an inline element, whereas a div is a block element. Commented Sep 23, 2018 at 20:37

3 Answers 3

2

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. uls or ols) can only have lis as their children, but lis 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

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

3 Comments

op isn't talking about appending new item to list. The problem is that the elements isn't appearing inline. fyi I'm not the down voter
You are not fixing the invalid HTML OP creates.
@connexo added note referencing list elements only having lis as their children, including a reference link, and updated my li_string variable.
1

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

  • inslide , this is not actualy right, this is not critical, it will work, but
  • must be inside

  • Comments

    1

    It seems from you code. You want to print div below li. There are few things to note:

    1. Li should always come inside UL or OL tag. You current code is semantically wrong.
    2. There can be only Li come as sibling of Li. No any other tag (You are using div)
    3. For position use css.

    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>

    2 Comments

    I want row and test to be on the same line, your code still puts it below
    fixed the solution. You have to just change css to display: inline on couple of places.

    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.