Javascript snippet does not work

Asked

Viewed 34 times

0

I don’t know what I’m doing wrong, maybe I can even be simple, but it doesn’t give me the expected result

When creating the html component:

 <li class="dropdown notifications-menu">

                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                    <i class="fa fa-university"></i>
                    <span class="label label-warning">{{count($empresas)}}</span>
                </a>

                <ul class="dropdown-menu">

                    <li class="header">Você pode emitir recibo para {{count($empresas)}} empresas</li>

                    <li>

                        <ul class="menu">

                            @foreach($empresas as $e)
                                <li><!-- start notification -->
                                    <a name="itemlista" id="{{$e->id}}"
                                       onclick="guardaIdSplit({{$e->id.":".$e->name}})" href="#">
                                    <i class="fa fa-industry text-aqua"></i> {{$e->name}}
                                </a>
                            </li><!-- end notification -->
                            @endforeach

                        </ul>

                    </li>

                </ul>

            </li>

In the script:

 function guardaIdSplit(empresa) {

                    var newArray = empresa.split(":");

                    var id = newArray[0];
                    var name = newArray[1];

                    $("[name=itemlista]").removeClass("text-green");
                    $("[name=itemlista]").addClass("text-black");

                    window.localStorage.setItem('emitente_id', id);

                    $("#" + id).removeClass("text-aqua");
                    $("#" + id).addClass("text-green");

                    $("#emitenteSelect").text(""+name);

                }

It should be close to this image:

inserir a descrição da imagem aqui

  • But what problem occurs? Does not run? Some error msg appears in the console?

  • Within the stretch had put up an Alert and does not perform, I do not know why... next to the dropdow was to fill in the company name... just to show the selected company..

  • and nothing happens

  • The snippet is the correct form the variables, the array and everything? I passed this way in onclick="guardaIdSplit({$e->id.":". $e->name}})"

  • When I click on the house I choose a company in the list and it should change where is microsoft for the selected company!!

1 answer

1


Your JS function is without syntax flaws, so the problem is probably in the call. Analyzing by what you put above:

Your code: onclick="guardaIdSplit({{$e->id.":".$e->name}})"

Must render: onclick="guardaIdSplit(1:Teste)"

When you should pass a string: onclick="guardaIdSplit('1:Teste')"

Therefore the solution must be: onclick="guardaIdSplit('{{$e->id.":".$e->name}}')"

  • Thank you very much, I hadn’t seen that question!!

  • How do I get this guard method to run, so that it gets the first element of the list? without the user going there on the dropdow and choosing an item.. Only for boot purposes..

  • Even when I can use the value of the variable in this method window.localStorage.setItem('issuer_id', id), that is until when it becomes available... for example if I click on a link that shows a list and updates the page, I lose the value of that variable?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.