0

I have been reading everywhere for a solution and I have tried all of them. But I am getting and error with this snippet:

if (jQuery('.page-id-5356 .uts-main-login')[0]){
  jQuery('.entry-content #em-wrapper').add('<div class="uts-login">text</div>');
}

Any ideas on why this isn't working? I either get nothing, a length of 2 or a geryed out undefined. No new div is created or anything. Basically, I want a a button to appear when on one page when .uts-main-login is visible. It's killing me! Been at it for hours :(

Ollie
  • 21
  • 1
  • 7
  • Possible duplicate of [Append div to end of document with jQuery?](https://stackoverflow.com/questions/6650963/append-div-to-end-of-document-with-jquery) – Zze Mar 08 '18 at 04:24

3 Answers3

1

Use like below

if (jQuery('.page-id-5356 .uts-main-login').length){
 jQuery('.entry-content #em-wrapper').append('<div class="uts-login">text</div>');
}
Zze
  • 18,229
  • 13
  • 85
  • 118
fayis003
  • 680
  • 4
  • 10
1

Use append

if (jQuery('.page-id-5356 .uts-main-login')[0]){
  jQuery('.entry-content #em-wrapper').append('<div class="uts-login">text</div>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='page-id-5356'>
<div class='uts-main-login'></div>
</div>
<div class='entry-content'>
<div id='em-wrapper'></div>
</div>
4b0
  • 21,981
  • 30
  • 95
  • 142
0
if(jQuery('.page-id-5356 .uts-main-login').is(":visible")) {
    jQuery('#em-wrapper').append('<div class="uts-login">text</div>');
}
  1. You can use is(':visible') to check whether the element is visible or not.
  2. If you have the id of the element no need to use class name before it.
  3. Use append() to add the element
Prabhat Rai
  • 799
  • 10
  • 12