I
I
Ilya17912017-03-20 16:56:45
CMS
Ilya1791, 2017-03-20 16:56:45

Why doesn't jquery work in the component template script in Bitrix?

Good afternoon!
I wanted to improve the form for adding an element to the Bitrix infoblock using custom scripts.
For these purposes, I used the script.js file in the component template. And it turned out that only pure javascript works in script.js. When trying to write something in jquery, an error is thrown in the console ReferenceError: $ is not defined.
There is jquery on the site, if you insert the code into the page, it works.
Why are functions not defined inside the Jquery component, and how to fix it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Gritsuk, 2017-03-20
@winer

How did you include jQuery in the header? Such errors are often due to the fact that they do not use api bitrix to connect scripts and styles.
Try to include your js libraries via addJs

use Bitrix\Main\Page\Asset;

Asset::getInstance()->addJs(SITE_TEMPLATE_PATH . "/js/jquery.js");

N
Nikita, 2017-03-20
@Rema1ns

Perhaps you have connected jiquery at the very bottom of the page, and those scripts where you write will be connected at the top of the page. Or you forget to enclose your code in a redis document.
Zhikveri can be connected from the Bitrix core
CJSCore::Init(array("jquery"));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question