1

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!

4
  • 1
    Make sure that the function traduzir is defined in the html before it is called. Commented Jan 5, 2015 at 12:53
  • This really calls for a switch statement. See here w3schools.com/js/js_switch.asp Commented Jan 5, 2015 at 12:57
  • Approaching your problem from a different point of view, can you do the translation server side in PHP before it's even rendered in the page? Commented Jan 5, 2015 at 12:57
  • Also, you have a typo where you use the vars woord and word Commented Jan 5, 2015 at 13:18

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How do I use the returned value?
@Ana do you want to print it at the position you call the script ?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.