0

I am using Jquery and Jquery-UI 1.7.2. The code itself is a tampermonkey script with the grants necessary for GM_setValue and GM_getValue already written. The part of the code that has the issue is the part where the select element should get updated to have the selected option as the option saved in GM at the start of the document. The select element simply doesn't show the saved option and stays on the default first option. I have tried nearly everything and have no idea what is wrong with the code.

I have tried debugging my code and tried using different methods to achieve what I wanted but nothing seems to work. I've also checked other problems similar to this but the solutions on the other posts haven't seemed to work in my case.

<div class="feature-select" id="element-type">
      <select>
        <option value="water" selected>Water</option>
        <option value="earth">Earth</option>
        <option value="land">Land</option>
        <option>
      </select>
</div>

$('.feature-select').each(function() {
    this.value = GM_getValue(`velocity.data.${$(this).attr('id').split("-")[0]}.type`, "")
    $(this).addEventListener('change', function (e) {GM.setValue(`velocity.data.${$(this).attr('id').split("-")[0]}.type`, e.target.value); console.log(e.target.value)});
})

1
  • Welcome to Stack Overflow. This does not appear to be jQuery UI based, per your tags. Please provide a minimal reproducible example and it would be good to take the tour.
    – Twisty
    Commented Oct 26, 2023 at 15:39

1 Answer 1

2

Looks like you are trying to change the value of the outer container. specifically the with the class .feature-select. However you should target the inner element to update its value.

You're using both the old (GM_getValue) and the new (GM.setValue) Tampermonkey APIs together.you should stick to one version, either all old-style or all new-style methods for a more efficient approach. Hope this helps

2
  • Thanks for the above and am now checking the actual select element instead of the container div. However now the value becomes nothing when set but use console.log on what it's being set to is a different value. How do I fix this?
    – deniz
    Commented Oct 26, 2023 at 12:05
  • @deniz please update your question to show what you have already tried in a minimal reproducible example. Commented Oct 26, 2023 at 13:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.