4

I've got a booking form.booking-wrap with 2 elements in it, div#booking and then div.quantity

<form class="booking-wrap"> 
    <div id="booking"></div>
    /* insert div#message IF div.quantity is present inside div.booking-wrap */ 
    <div class="quantity"></div>
</form>

The div.quantity is only present dynamically for some bookings.

What I am trying to achieve is that if the div.quantity is present, then I would like to insert an additional html div#message, but this new div should appear after div#booking and before div.quantity

I am trying the following jQuery:

if($('.booking-wrap:has(div.quantity)')){
  $('#booking' ) .after( '<div id="message">Message</div>');
}

But that doesn't seem to work.

I then tried this:

$('.booking-wrap:has(div.quantity)').append($('<div id="message">Message</div>'));

This works and the new div appears, however it is just next to the quantity div.

How can I get it to show after the #booking , but before .quantity?

0

5 Answers 5

1

Any selector returns an object, you should check the length property of the returned object. Like $('.booking-wrap:has(div.quantity)').length

Though, I prefer $('.booking-wrap > div.quantity').length

if($('.booking-wrap > div.quantity').length){
  $('#booking').after('<div id="message">Message</div>');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="booking-wrap">
  <div id="booking">booking</div>
  <div class="quantity">quantity</div>
</form>

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

2 Comments

Btw what does the length property do?
@JoeBloggs, the selector returns object, that is why you have to check the length property:)
1

Try using prepend instead of append, since the selector $('.booking-wrap:has(div.quantity) will return the (div.quantity) element.

Example:

$('.booking-wrap:has(div.quantity)').prepend($('<div id="message">Message</div>'));

2 Comments

Many thanks Nikola, I'll go with Mamun's answer for this one ..
Using prepend is simpler and cleaner, but I don't mind :"D Best of luck with your project.
0

You can use "hasClass()" jQuery function to check class is exist or not:

Try This

if($( ".booking-wrap" ).children('div').hasClass( "quantity" )){
  jQuery('<div id="message">Message</div>').insertAfter('.booking');
}

Comments

0

Try This jQuery.

if($(".quantity").length ){
    $('#booking' ).after( '<div id="message">Message</div>');
}

Comments

0

You need to put the div you are adding within a function.

        if($('.booking-wrap:has(div.quantity)')){
            $( "#booking" ).after(function() {
                return '<div id="message">Message</div>';
            });
        }

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.