Why does a function show up as not defined

300 views Asked by At

XSL:

    <script type="text/javascript">
        <xsl:value-of select="concat('replaceAll(',$idS,');')"/>
    </script>

HTML:

<script type="text/javascript">replaceAll(ID0ED);</script>

Javascript:

$(function () {
    var cityNameRye = $("#spnCityID0ED").text().split(";")[1];
    if (cityNameRye.toLowerCase() == "rye") {
        //do something
    }
});

I get the following error in the console which causes an issue in the site:

Uncaught ReferenceError: replaceAll is not defined

Can I create a dummy function which clears out the error?

2

There are 2 answers

2
SzybkiSasza On BEST ANSWER

Put this function code somewhere when it could be loaded before those from XSL are executed:

function replaceSpnCity(idPart) {
  var idsToReplace = $("[id^='spnCity']"); //updated missing quote
  idsToReplace.each(function() {
    var id = $(this).attr('id');
    var newId = id.replace(idPart,'');
    $(this).attr('id',newId);
  });
}

And call it from XSL like:

<xsl:value-of select="concat('replaceSpnCity(&apos;',$idS,'&apos;);')"/>
4
Lau On

You do not have any function with the name replaceAll() in your code

try

function replaceAll(varName){
   //code to execute
}

and see if you still have the error