1

I'm trying to get the value of a textarea with Shopify Product Options by Bold and it is currently not working. I am able to get the value of a textarea locally, but I can not get the value when I move the code over to Shopify. I have looked here and here to no avail.

Here's the relevant code:

document.getElementById("248832").onkeyup=function(){getVal()};
var textbox = document.getElementsByName("properties[Message Body]")[0].value;

and here's the textarea I'm trying to get the value of

<textarea data-option-key="ta_248832" id="248832" class="bold_option_child shapp_full_width " name="properties[Message Body]"></textarea>

When I try to run this on Shopify, I get an error saying "Uncaught TypeError: Cannot set property 'onkeyup' of null", although I did notice that at one point shopify runs the following jQuery code, which might be what's causing my problem:

<script>
jQuery('#248832').change(function (){conditional_rules(7437760391);})
</script>

I am trying to get the value of the textarea so I can run get the amount of words in said textarea.

Any help would be greatly appreciated!

Community
  • 1
  • 1

2 Answers2

0

Solution

You are trying to use something similar to jQuery methods without jQuery:

document.getElementById("248832").onkeyup=function(){getVal()};

while DOM elements don't have the onkeyup method. It should be

jQuery("#248832").on("keyup",function(){getVal()});

Extra notes

even without using the recommended '.on' method, you have to write it as

jQuery("#248832").keyup(function(){getVal()});

(docs), not as

jQuery("#248832").onkeyup(function(){getVal()});

If you'd like to use DOM methods, that would be

document.getElementById("248832").addEventListener("keyup",function(){getVal()});

Also, may be you have wrote this as a minimal example, but the fact that you only call getVal() and don't pass the value somewhere sounds strange, although I don't know if what getVal() does exactly.

YakovL
  • 7,557
  • 12
  • 62
  • 102
  • Just tried it with jQuery and with the DOM Method to no avail. Got the following error with the DOM Method: `Uncaught TypeError: Cannot read property 'addEventListener' of null` – Jose Maldonado Jul 02 '16 at 19:23
0

Look to see if the element that is returned as null has loaded yet. Others in similar situations fixed this by loading the script last. Hope this is helpful :)

Community
  • 1
  • 1
anonameuser22
  • 108
  • 12