T
T
turindd2021-07-15 11:04:29
JavaScript
turindd, 2021-07-15 11:04:29

How to pass an array to js?

Hello.
It is necessary to pass an array to JS

An array comes from the controller (collection). i get all id there js script
$top_menu->pluck('id')
60efeb7bafc05101733834.jpeg

// 1 часть меню
but_g_menu. onclick = function() {
    if(id_g_menu. style . display == "none")  {
        id_g_menu.style . display= "";
    }
    else  { id_g_menu.style . display = "none"; }
}
// 2 часть меню
var id_menu = new Array('1111','2222','3333');
startList = function allclose() {
    for (i=0; i < id_menu.length; i++){
        document.getElementById(id_menu[i]).style.display = "none";
    }
}
function openMenu(id){
    for (i=0; i < id_menu.length; i++){
        if (id != id_menu[i]){
            document.getElementById(id_menu[i]).style.display = "none";
        }
    }
    if (document.getElementById(id).style.display == "block"){
        document.getElementById(id).style.display = "none";
    }else{
        document.getElementById(id).style.display = "block";
    }
}
window.onload=startList;


you need to pass the category ID to js var id_menu = new Array(
or change js somehow differently.
I don’t understand js much.
Thanks in advance for any information!

I can pass it like this:
var id_menu = json_encode($top_menu->pluck(' id'));
var id_menu = json_encode([1,2,3,4,16,19]); but script doesn't work

var id_menu = new (json_encode($top_menu->pluck('id')));
var id_menu = new (json_encode([1,2,3,4,16,19])); doesn't work either

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2021-07-15
@turindd

var id_menu = @json($top_menu->pluck('id'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question