Uncaught Referenceerror when passing values via JS

Asked

Viewed 1,772 times

0

I’m trying to pass a string and an int in a JS for it to return me in a modal and I see this error below, someone could help me?

Trying to pass values here on td to JS and he needs to return it to me in modal. Obs: using java spring to bring this values from the database

<td><a href='#' onclick="setaDadosModal(nome, numero)" data-toggle="modal" data-target="#con-close-modal-altera"><i class="fa fa-pencil"></i></a></td>

My modal

<div id="con-close-modal-altera" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title">Alterar Modalidade</h4>
                    </div>
                    <form name="alterarModalidade" action="alteraModalidades" method="post" role="form"> 
                        <div class="modal-body">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <input required="" name="idModalidades" type="hidden" id="alteraIdModalidade" class="form-control"> 
                                        <input required="true" name="nomeModalidadeTemp" type="text" id="alteraNomeModalidadeTemp" class="form-control" id="exampleInputEmail1" placeholder="Digite a modalidade a ser alterada" maxlength="30"> 
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Fechar</button>
                            <button type="submit" onclick="return alterarModal()" class="btn btn-warning waves-effect waves-light">Alterar</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

My JS

function setaDadosModal(AltnomeModalidade, AltidModalidade) {
document.getElementById('alteraNomeModalidadeTemp').value = AltnomeModalidade;
document.getElementById('alteraIdModalidade').value = AltidModalidade;}

My Mistake

modalities:258 Uncaught Referenceerror: aaaa is not defined at Htmlanchorelement.onclick (modalities:258)

Row 258 the error refers to

<td><a href='#' onclick="setaDadosModal(aaaa, 107)" data-toggle="modal" data-target="#con-close-modal-altera"><i class="fa fa-pencil"></i></a></td>

1 answer

0


In the HTML referred by you, which is the source of the error:

 <td><a href='#' onclick="setaDadosModal(aaaa, 107)" data-toggle="modal" data-target="#con-close-modal-altera"><i class="fa fa-pencil"></i></a></td>

Let’s extract and focus on that part (which is Javascript):

setaDadosModal(aaaa, 107)

Note that in the first parameter, you passed a variable, however, as this variable (aaaa) does not exist, the error is being played.

Probably, instead of passing a variable in the first parameter, you wanted to pass a string, but for that, you should have done:

setaDadosModal('aaaa', 107) // Recomendado

Or alternatively:

var aaaa = 'aaaa'; setaDadosModal(aaaa, 107)

Then staying in HTML:

 <td><a href='#' onclick="setaDadosModal('aaaa', 107)" data-toggle="modal" data-target="#con-close-modal-altera"><i class="fa fa-pencil"></i></a></td>

Or:

 <td><a href='#' onclick="var aaaa = 'aaaa'; setaDadosModal(aaaa, 107)" data-toggle="modal" data-target="#con-close-modal-altera"><i class="fa fa-pencil"></i></a></td>
  • 1

    Man, thank you so much for the help and explanation, it was just that, what a silly little mistake.

Browser other questions tagged

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