0

I'm trying to create a simple javascript code in odoo 10. So Under myModule\static\src\js folder I added a test.js file containing this code:

alert("it works!!");

And Under myModule\views I added an xml file containing this code:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
    <template id="assets_backend" name="solixy assets" inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script type="text/javascript" src="/solixyProject/static/src/js/test.js"></script>
        </xpath>
    </template>
</data>

And it works fine when I update my module and refresh odoo. Now I want this js code to be called when I press a button. So I changed js code like this:

function clickMe(){
    alert("it works!!");
};

Then in my form I added this button:

<button name="test_js" string="Test Javascript File" type="object" class="btn-primary" onclick="clickMe()"/>

Now the button is shown but I don't have my alert box!!

Thank you for your help.

2
  • Can you check your browser console to ensure the JS is loaded and to see if it is throwing any errors? Commented Jan 6, 2018 at 5:30
  • Yes I got this error: Unreachable code after return statement Commented Jan 8, 2018 at 14:24

1 Answer 1

0

It looks like I'm missing many other javascript code so my method can work. We can't just add a method like this:

function clickMe(){
    alert("it works!!");
};

This is the correct js code:

odoo.define('solixyProject.kanban_view', function (require){
"use strict";
var form_widget = require('web.form_widgets');
var core = require('web.core');
var _t = core._t;
var QWeb = core.qweb;

form_widget.WidgetButton.include({
    on_click: function() {
         if(this.node.attrs.custom === "click"){

            alert("It works!!");

            return;
         }
         this._super();
    },
});
});

And this is the correct xml code:

<button string="Test Javascript Onclick Method" custom="click" />

This question helped me a lot: Odoo javascript onclick event

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

1 Comment

how did u solved the problem here? TypeError: exec_workflow() takes exactly 4 arguments (3 given)

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.