0

i have a problem on window resize. I should to append one time on resize an element to anather div but it appedn more times. How can i fix it ? Here you can find an example, resize window more time and after that click on first div and you will see the problem :

$(window).on('resize', function() { var windowW = $(window).width(); console.log(windowW); if(windowW <= 670){ $('.take').on('click touchstart', function(){ var info = $(this).text(); $('.add').append('<p>Inormation :'+info+'</p>'); }); } else { $('.add').empty(); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <div class="take">Some information </div> <div class="add"> </div>

12
  • Why do you want to use window resize if you want to append it on click? Commented May 7, 2018 at 8:11
  • @KrzysztofJaniszewski i have an complex code and this is just an example. So i want to have a answer to my question. Commented May 7, 2018 at 8:13
  • you want to append element on click right Commented May 7, 2018 at 8:14
  • but only one time Commented May 7, 2018 at 8:15
  • @patelarpan yes on window resize. Commented May 7, 2018 at 8:15

1 Answer 1

1

Just check the width before append

$('.take').on('click touchstart', function(){ var info = $(this).text(); var windowW = $(window).width(); if(windowW <= 670){ $('.add').append('<p>Inormation :'+info+'</p>'); } else { $('.add').empty(); } }); 

Example here

Updated

//Disapear when >= 680 $(window).on('resize', function() { var windowW = $(window).width(); if(windowW >= 680){ $('.add').empty(); } }); 

Check size want to disapear

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

1 Comment

if size is 680 it should to dessaper. Thats why i want to qrite it on resize. Your answer is on click but i want it on resize and after that click whats mean after resize you click on !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.