Good evening. In general, I need to copy the text from <div> to <meta name="description" content=""> using JQ. How to implement? Preferably with code and explanation.

  • + limit the text to a certain number of characters. - CbIPoK2513
  • ??? meta are headers. JS is inside. The question seems vague ... After all, after sending the headers to change them will not work ... - DNS
  • @DNS, meta tags and http request headers are, to put it mildly, different entities. - vp_arth

2 answers 2

It's simple, here's an example:

 var text = $('.text-item').text(); //Выбираем текст из div text = text.slice(0,10); //обрезаем текст с начала и длиной 10 символов $('meta[name=description]').attr('content', text) // вставляем в attr content текст 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <meta name="description" content=""> <div class="text-item">Задача организации, в особенности же постоянное информационно-пропагандистское обеспечение нашей деятельности играет важную роль в формировании модели развития. Таким образом постоянный количественный рост и сфера нашей активности позволяет оценить значение системы обучения кадров, соответствует насущным потребностям.</div> 

    Well, the vanilla version.

     const meta = document.querySelector('meta[name=description]'); const text = document.querySelector('#div').textContent; console.log(meta.getAttribute('content')); // !!! meta.setAttribute('content', text.slice(0, 20)); console.log(meta.getAttribute('content')); // Lorem ipsum dolor si 
     <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="description" content="!!!"> </head> <body> <div id="div">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore rem, doloremque et, inventore ipsam tenetur impedit, accusantium aliquid libero fugiat error voluptas dolor enim, laborum accusamus doloribus quam. Distinctio, sed.</div> </body> </html>