how to remove whitespaces and keep breaks without being able to edit the html?

You can simply use innerText

function strip() {    
  document.querySelector('#pstad-descrptn').value =
    document.querySelector('#item-info-container').innerText
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="button" id="thebutton" value="strip" onclick="strip();">
<textarea id="pstad-descrptn" name="description" data-val="true" cols="78" rows="20"></textarea>

<div id="item-info-container" class="clipboard copied re-copy" data-clipboard-target="#item-info-container">
                <div id="item-pre-description">
                                      <h2 class="h5">Product details:</h2>                      <div class="product-details__columns">                          <p class="product-details__column">                              <strong>Item Number:</strong>                              <span id="product-unique-id">000800209270</span>                          </p>                              <p class="product-details__column">                                  <strong>Brand:</strong>                                  <span>Nikon</span>                              </p>                              <p class="product-details__column">                                  <strong>Model Number:</strong>                                  <span>D3500</span>                              </p>                              <p class="product-details__column">                                  <strong>Colour:</strong>                                  <span>Black</span>                              </p>                              <p class="product-details__column">                                  <strong>Number of Lenses:</strong>                                  <span>2</span>                              </p>                      </div>                                  </div>
                <div id="item-description">
                This Nikon D3500 camera body comes with<br><br>Nikon 18-55mm 1:3.5-5.6G VR Lens<br>Nikon 70-300mm 1:4.5-6.3G ED VR Lens<br>1 battery &amp; charger.<br><br>Shutter count: 426<br><br>The camera and both lens are in perfect working order, and is showing hardly any visible wear (please see photos).<br><br>For more information on this or any other item you may have seen please feel free to send me an email and I will respond as soon as possible.<br><br>                </div>
                
            </div>


I want the pre-description to be a block of code with no spaces only line breaks.

You then need to separate the paragraphs and combine them (and eliminate double line ending at first paragraph)

function strip() {
  let item_pre_desc = document.querySelector('#item-pre-description')
  let item_desc = document.querySelector('#item-description')
  let result = document.querySelector('#pstad-descrptn')
  result.value = 
    item_pre_desc.innerText.replace(/\n+/g,'\n') 
    + '\n'
    + item_desc.innerText
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="button" id="thebutton" value="strip" onclick="strip();">
<textarea id="pstad-descrptn" name="description" data-val="true" cols="78" rows="20"></textarea>

<div id="item-info-container" class="clipboard copied re-copy" data-clipboard-target="#item-info-container">
                <div id="item-pre-description">
                                      <h2 class="h5">Product details:</h2>                      <div class="product-details__columns">                          <p class="product-details__column">                              <strong>Item Number:</strong>                              <span id="product-unique-id">000800209270</span>                          </p>                              <p class="product-details__column">                                  <strong>Brand:</strong>                                  <span>Nikon</span>                              </p>                              <p class="product-details__column">                                  <strong>Model Number:</strong>                                  <span>D3500</span>                              </p>                              <p class="product-details__column">                                  <strong>Colour:</strong>                                  <span>Black</span>                              </p>                              <p class="product-details__column">                                  <strong>Number of Lenses:</strong>                                  <span>2</span>                              </p>                      </div>                                  </div>
                <div id="item-description">
                This Nikon D3500 camera body comes with<br><br>Nikon 18-55mm 1:3.5-5.6G VR Lens<br>Nikon 70-300mm 1:4.5-6.3G ED VR Lens<br>1 battery &amp; charger.<br><br>Shutter count: 426<br><br>The camera and both lens are in perfect working order, and is showing hardly any visible wear (please see photos).<br><br>For more information on this or any other item you may have seen please feel free to send me an email and I will respond as soon as possible.<br><br>                </div>
                
            </div>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top