Search code examples
typeerrorrangy

Object <object> has no method 'methodeX'


i get an error in chrome:

Uncaught TypeError: Object s1A has no method 'applyToSelection'

in Firefox(firebug) i get this:

TypeError: val.applyToSelection is not a function

i use the rangy-core and the rangy-cssclassappliere

my Code:

<script type="text/javascript"> 
    window.onload = function() {    
    rangy.init();
      s1A = rangy.createCssClassApplier("css_1", true); 
    };

   //this is the Problem.
   function dosome (el) {
      var val = el.value;
      val.applyToSelection();
   }
</script>

<body>
  <button value="s1A" onclick="dosome(this);">test</button>
</body>

if id do this:

<script type="text/javascript"> 
    window.onload = function() {    
    rangy.init();
      s1A = rangy.createCssClassApplier("css_1", true); 
    };
   function s1() {
      s1A.applyToSelection();
   }
</script>

<body>
  <button onclick="s1();">test</button>
</body>

it works fine. But i need it for an option field and i want to get the value


Solution

  • I suggest storing appliers in an object where each property name corresponds to an input value:

    window.onload = function() {
        var appliers = {};
        rangy.init();
        appliers.s1A = rangy.createCssClassApplier("css_1", true); 
    };
    
    function dosome(el) {
        var val = el.value;
        appliers[val].applyToSelection();
    }