3

I have the following javascript where I am reading in a word and writing out a translation, but I keep getting the error boolean is not a function

function translate() {
var word = $("#prodId").val();

$.getJSON("api/translation?word=" + word,
            function (data) {

                $("#word").text(data.TranslatedWord);
            })
            .fail(
                function (jqXHR, textStatus, err) {
                    $("#word").text('Error: ' + err);
                });
                    }

The following method which is basically the same thing, but uses an integer works fine:

function find() {
       var id = $("#prodId").val();
       $.getJSON("api/products/" + id,
            function (data) {
                var str = data.Name + ': $' + data.Price;
                $("#product").text(str);
            })
            .fail(
                function (jqXHR, textStatus, err) {
                    $("#product").text('Error: ' + err);
                });
                    }

Here is a snippet of the HTML:

<div id="body">
    <div class="main-content">
        <div>
            <h1>All Products</h1>
            <ul id="products" />
        </div>
        <div>
        <label for="prodId">ID:</label>
        <input type="text" id="prodId" />
        <input type="button" value="Translate" onclick="translate();" />
        <p id="word" />
        </div>
    </div>
</div>

<script type="text/javascript">
$(document).ready(function () {
    // Send an AJAX request
    $.getJSON("api/products/",
              function (data) {
                  // On success, 'data' contains a list of products.
                  $.each(data, function (key, val) {

                      // Format the text to display.
                      var str = val.Name + ': $' + val.Price;

                      // Add a list item for the product.
                      $('<li/>', { text: str })
                      .appendTo($('#products'));

                  });

              });

});

    function find() {
        var id = $("#prodId").val();
        $.getJSON("api/products/" + id,
                  function (data) {
                      var str = data.Name + ': $' + data.Price;
                      $("#product").text(str);
                  })
        .fail(
            function (jqXHR, textStatus, err) {
                $("#product").text('Error: ' + err);
            });
    }

    function translate() {
        var word = $("#prodId").val();

        $.getJSON("api/translation?word=" + word,
                  function (data) {

                      $("#word").text(data.TranslatedWord);
                  })
        .fail(
            function (jqXHR, textStatus, err) {
                console.log(err);

                $("#word").text('Error: ' + err);
            });
    }




</script>
Barmar
  • 741,623
  • 53
  • 500
  • 612
Xaisoft
  • 45,655
  • 87
  • 279
  • 432

3 Answers3

7

It seems that in chrome all elements have a boolean property called translate, (e.g. console.log(document.body.translate) will display true in chrome, not sure why.

When you do onclick="translate();" then it simply calls it on the local DOM object scope (now why doesn't it call it on the window object is another question)

e.g. if you change translate to translate2 it should work, as weird as it sounds

Eran Medan
  • 44,555
  • 61
  • 184
  • 276
  • 1
    translate is a reserved word in chrome. Changing it to translateWord worked. – Xaisoft Apr 16 '13 at 20:06
  • yep, weird, I couldn't find any documentation on it, except this related SO question: http://stackoverflow.com/questions/13113914/removing-chromes-translate-dom-property – Eran Medan Apr 16 '13 at 20:16
4

It seems like the onclick isn't working.

Since you are already using jQuery I would suggest using the click event instead of having the onclick in the HTML. It is better practice to do so...

Try something like this:

$("#translateButton").click(function() {
    translate();
});

And simply your HTML to this:

<input id="translateButton" type="button" value="Translate" />
chue x
  • 18,573
  • 7
  • 56
  • 70
c0deNinja
  • 3,956
  • 1
  • 29
  • 45
  • The symptom suggests that something is redefining `translate` from a function to a boolean. Changing how the click handler is bound probably won't solve this. – Barmar Apr 16 '13 at 19:45
  • This actually worked, but why does it not work the other way? – Xaisoft Apr 16 '13 at 19:47
  • true... but from the code he has posted, we can't see what has redefined the translate function... so suggesting a different way of binding could solve it. – c0deNinja Apr 16 '13 at 19:48
  • @Xaisoft... not sure, which browser are you using? – c0deNinja Apr 16 '13 at 19:48
  • @c0deNinja - It works for the find function though, but not the translate function. – Xaisoft Apr 16 '13 at 19:52
  • 1
    Maybe rename translate to translateWord??? haha not sure, maybe chrome is using translate() – c0deNinja Apr 16 '13 at 19:58
  • lol, that has to be it, changing it to TranslateWord worked, so apparently translate is some reserved word in chrome. – Xaisoft Apr 16 '13 at 20:02
  • rename function name work for me( Ex: translatioe > googleTranslate), i think translate function try to overriding. i used this same code block for translation. – Chanuka Asanka Jul 22 '14 at 17:55
1

The problem is that you're defining your translate() function inside the document.ready() function. These function definitions are local to that scope, they can't be accessed from onclick.

You don't need to put function definitions inside the ready handler, you only need statements that access the DOM immediately in there. Define it at toplevel and it should work.

Or, as c0deNinja suggested, use jQuery to bind the handler. If you do this inside the ready handler, it can call other functions defined in there.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I took out the document.ready and the other function. When running it in IE, it works, but in chrome, it does not. – Xaisoft Apr 16 '13 at 19:55
  • Thought so too, it's not, this is a tricky one see this http://jsfiddle.net/SR8Z3/7/ and look at the console – Eran Medan Apr 16 '13 at 19:58