Execute script after clicking in field

The original page is absolutely and in all identical to the saved uploaded page. The only difference is that in the original page “Titolo” area is empty, and I manually type in it the text.

Sorry man, we’re going round in circles. The script I provided works on the page you just linked to.

If I can’t reproduce the problem you are having, it’ll be difficult for me to help :frowning:

I see and appreciate your effort, thanks really, I don’t know how else to reproduce the problem…

1 Like

James_Hibbard, what about this page?

Ok, I understand what you mean now. Thanks for making a page that demonstrates your problem.

So the reason that “Prima data” is filled out with undefined the first time you click into it is because “Titolo” contains nothing and calling findYear(titoloText); when titoloText is an empty string doesn’t return anything, so it is therefore undefined.

So the question is, how should the script behave if “Titolo” is empty and the user clicks into “Prima data”? What should happen in this case?

You’re welcome. These are the steps:

  1. In Titolo (empty at the beginning) I type manually the text.
  2. I click in primaData, and the script should run, but here I get ‘undefined’ even if I had first manually filled up the Titolo field.

If Titolo is empty and the user clicks into “Prima data” nothing should happen and no value should be set.

This’ll work.

(function() {
  'use strict';

  const titolo = document.querySelector('#dsArea0');
  const primaData = document.querySelector('input[name="annoPbb1"]');

  primaData.addEventListener('click', function() {
    if (titolo.value) this.value = findYear(titolo.value);
  }, { once: true });

  function findYear(str) {
    const strArray = str.split(" ");

    for (let i = 0; i < strArray.length; i++) {
      const currentElement = strArray[i];

      if (currentElement.includes(".")) {
        const yearPoint = currentElement.split(".");
        const year = yearPoint[0];

        if (year > 1800) {
          return year;
        }
      }
    }
  }
})();
1 Like

Wao, now it finally works perfectly, thanks very much James_Hibbard, and sorry for my bad explanations!! Thank again!!!

Happy to help. Let me know if you need to alter it to do anything else :slight_smile:

1 Like

I’ll let you know, thanks so much!

1 Like

Hi, James_Hibbard, is it possible a little variation? I would like primaData to be filled by the year after first click on Numero field, not on primaData. Thanks again!

Sure, then attach the event handler to that field instead.

This is untested, but should work:

(function() {
  'use strict';

  const titolo = document.querySelector('#dsArea0');
  const primaData = document.querySelector('input[name="annoPbb1"]');
  const numero = document.querySelector('input[name="nstd"]');

  numero.addEventListener('click', function() {
    if (titolo.value) primaData.value = findYear(titolo.value);
  }, { once: true });

  function findYear(str) {
    const strArray = str.split(" ");

    for (let i = 0; i < strArray.length; i++) {
      const currentElement = strArray[i];

      if (currentElement.includes(".")) {
        const yearPoint = currentElement.split(".");
        const year = yearPoint[0];

        if (year > 1800) {
          return year;
        }
      }
    }
  }
})();
1 Like

Great, works really good, thanks so much James!!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.