Problems with selector and jQuery

Asked

Viewed 127 times

0

I’m trying to run a code but it doesn’t work, I don’t know if it’s because of a logic error in its execution, see, this code runs when the button OK is pressed:

$('HTMLNEW').appendTo($(this).closest('.BOX').find('.DIVNEW')).css({
    animation: "---"
});

the HTML:

<div class="BOX">
    <div class="DIVNEW">

    </div>    
    <button id="ok">OK</button>
</div>

but it does not perform what is asked, indeed it does not perform anything.

  • 4

    What is this $('HTMLNEW')?

  • @Sergio codes are mere representations, HTMLNEW are codes HTML any, how <div id="...

  • You want to insert a new element/content within the divnew and then manipulate the css of this new element/content? That’s it? Your question is very confusing.

  • 1

    Are you sure this selector HTMLNEW is there? per case this would not be a variável, #id or .classe? Personally I’ve never seen any reference to that HTMLNEW in documentations

  • Your code works... https://jsfiddle.net/vo5bco1r/ I don’t understand your problem... you can adapt jsFiddle and explain better?

  • The question doesn’t really make sense, why it doesn’t work for me is related to this since this was executed within a setInterval.

Show 1 more comment

1 answer

2

Use the function append to insert the html into the desired element and use the function Prev to select the item before your button.

Note that the append will always add content at the end of element, not replacing what already exists, if you want to replace the that you already use the function html, if you want to add at the beginning, use the prepend.

$('#ok').on('click', function () {
  var html = '<h1>Texto adicionado com append</h1>';
  
  $(this).prev('.divNew')
    .append(html)
    .css('color', 'red');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">
  <div class="divNew">
  </div>
  <button id="ok">OK</button>
</div>

  • The AP code works (https://jsfiddle.net/vo5bco1r/). There’s a problem there that needs fixing?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.