Thanks you for help.
Code as above works perfectly and as expected if placed after a document ready function in header.php.
If I save same code in a xxx.js file and then I enqueue the script the correct way, it doesn’t work…Should I modify the code one way or another to make it work when loaded from a specific file ?
As you advise I can then use conditional tags to load it only for pages or even posts I choose.
Any ideas ?