Home > Back-end >  How to completely remove spaces in copied text in Javascript
How to completely remove spaces in copied text in Javascript

Time:11-19

I put a javascript copy button, but this button takes all the spaces in the html. How can I solve this?

JavaScript File:

$(document).ready(function() {
  $('button').click(function(){
    var btntxt = $(this).text();
    
     var copy = $(this).parent().find('.copy').text();

     
     var $temp = $("<input>");
     $("body").append($temp);
     $temp.val(copy).select();
     document.execCommand("copy");
     $temp.remove();
     
    
    
    $('.confirmation').hide().html('<b>'   btntxt   ' </b> Kopyalandı! ').fadeIn(100).delay(1200).fadeOut(200);
   
    $( '.main' ).trigger( "click" );
  });
  
  $('.main div').click(function(){
      var range = document.createRange();
      var selection = window.getSelection();
      range.selectNodeContents(this);
      selection.removeAllRanges();
      selection.addRange(range);
  });
});

Html File:

<div >
            <button >
                <span >
                    <span >Text will come here</span>
                </span>
                <button >
                    <text >Also Text will come here too</text>
                    <i  aria-hidden="true"></i>
                </button>
            </button>
</div>

Also i tried to add remove spaces code but it didn't work.

CodePudding user response:

replace this line
var copy = $(this).parent().find('.copy').text();
with this one
var copy = $(this).parent().find('.copy').text().replace(" ", "");

  • Related