0

I tried to show some text into javascript but unable to do

First of all, here is the code

$('.example').each(function() {
  var $t = $(this),
    $w = $t.find('.widget.Text');
  if ($w != undefined) {
    months = $(this).text().replace($w, 'months');
    days = $(this).text().replace($w, 'days');
    months != false && $t.find('#example-done').text(months);
    days != false ? days = Number(days) : days = 7;
  }
});
// up to so...............on
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="example">
  <div class="widget Text">
    months=(Accept) days=(20)
  </div>
</div>

Here in the above code months and days data shows I tried to implement this data in javascript

months=(Accept) its value should be Accept in Js

days=(20) its value should be 20 Accept in Js

I tried the above method but not working

is there any method available to insert the above HTML format data in my Javascript

I think .split() function will resolve this problem please provide any guide (like we have to split both data with help of js and Text Trim)

Any help is highly appreciated

I just want to show months and days values into my javascript code, Please provide any code to fix this issue. I tried hard but I am unable to fix it. Like months=(Accept) and days=(20), Now how to show that value (20 and accept) into javascript? Please leave other code which I provide above Just guide me on how to show this HTML format data into js

  • The `widget text` element doesn't have `data-shortcode`. – Barmar May 20 '21 at 01:19
  • What are you expecting `$t.replace($s, 'months')` to do? `$t` is a jQuery object, it doesn't have a `.replace()` method. Do you mean `$t.text().replace($s, 'months')`? – Barmar May 20 '21 at 01:23
  • I do not see a Data attribute anyplace. – Twisty May 20 '21 at 01:23
  • Provide a proper explanation of what you want to accomplish along with expected results. There are numerous issues with the code shown. Broken code is not a good substitute for a proper explanation. See [mre] – charlietfl May 20 '21 at 01:32
  • Thanks for your response, Dear Respected sir I just want to show months and days values into my javascript code, Please provide any code to fix this issue. I tried hard but I am unable to fix. Like months=(Accept) and days=(20), Now how to show that value (20 and accept) into javascript ? Please leave other code which I provide above Just guide me how to show this HTML format data into js – Pritika Dhiman May 20 '21 at 01:35
  • What does "show months and days values into javascript code" mean? – Barmar May 20 '21 at 01:45
  • `$w` will never be `undefined`. `$("selector")` always returns a collection of elements, possibly empty. Do you mean `$w.text()`? – Barmar May 20 '21 at 01:47

2 Answers2

1

Use .text() to get the contents of the DIV. Then you can use a regular expression to match the contents and extract the values.

$('.example').each(function() {
  var $t = $(this),
    $w = $t.find('.widget.Text').text();
  if ($w) {
    var match = $w.match(/months=\(([^)]*)\)\s+days=\((\d+)\)/);
    if (match) {
      var months = match[1];
      var days = Number(match[2]);
      console.log(`Months = ${months}, Days = ${days}`);
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="example">
  <div class="widget Text">
    months=(Accept) days=(20)
  </div>
</div>

See Reference - What does this regex mean? for explanations of the regular expression details.

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • Thanks for this code but please i really want to learn this rather then I make copy paste Please guide how you fix this very easily and what is this part = `$w.match(/months=\(([^)]*)\)\s+days=\((\d+)\)/);` Thanks in advance – Pritika Dhiman May 20 '21 at 01:58
  • @PritikaDhiman See https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean/22944075#22944075 – Barmar May 20 '21 at 02:01
0

Change

months = $(this).text().replace($w, 'months');
days = $(this).text().replace($w, 'days');
months != false && $t.find('#example-done').text(months);
days != false ? days = Number(days) : days = 7;

to

let a = $t.text().match(/months=\(([^(]*)\)\s+days=\(([^(]*)\)/);
months = a[1]; days = a[2];
days = days === '' ? 7 : +days;
StackSlave
  • 10,198
  • 2
  • 15
  • 30