My database is giving me week days as "0", "1", "3", "4", "5", "6". And I wanted to translate it to their actual name.
So I'm doing:
HMTL
<div><script>traduzir("<?php echo $row_room['weekday']?>")</script></div>
JS
<script>
function traduzir (woord){
if (woord == "0")
{
return "Domingo";
}
if (word == "1")
{
return "Segunda";
}
if (word == "2")
{
return "Terça";
}
if (word == "3")
{
return "Quarta";
}
if (word == "4")
{
return "Quinta";
}
if (word == "5")
{
return "Sexta";
}
if (word == "6")
{
return "Sabado";
}
};
</script>
I'm getting "traduzir is not defined" error. Can someone help? Thanks!
2 Answers 2
First you need to make sure the script that defines the traduzir method is placed before the script that calls it.
Then you need to also change your script so that it actually does something with the returned value.
2 Comments
If the section marked JS is in a separate JavaScript file, then remove the HTML (<script> and </script>).
In either case, move the second script so the HTML document loads it before the first script. You can't use a function until you have defined it, and it won't be hoisted from a later script in time to use it immediately in an earlier one.
traduziris defined in the html before it is called.