Call function by Onclick passing parameters

Asked

Viewed 4,167 times

0

I am trying to call a function by clicking on a link, where a parameter is passed that will be used in an Ajax request.

This request will return results that will be added to a div.

The link code with the function call is as follows:

<a href="javascript:;" id="Iniciais" onclick="pBuscaFichaEmergencia('<?php echo $Letra; ?>');"><?php echo $Letra; ?></a>

The function code is:

function pBuscaFichaEmergencia(){
    var Iniciais = $('#Iniciais').val(); 

    console.log("INICIAL: " + Iniciais);

    if (Iniciais) {
        var url = 'pBuscaFichasLetras.php?Iniciais='+Iniciais 
        $.get(url, function(dataReturn) {
            $('#resp-emergencia').html(dataReturn);  
        });
    }
}
  • You can explain better what should happen?

  • Hello @Sergio, when passing the value of the parameter of the variable "Initials" to the page "pBuscaFichasLetra.php" would make the query and return a formatted response in an html table positioned in the emergency divResp, but I cannot pass the value of this variable.

  • Which part does not work? on the server or browser?

  • What doesn’t work is in the Browser.

1 answer

4


If you will pass the letters by PHP this way

<a href="javascript:;" id="Iniciais"  onclick="pBuscaFichaEmergencia('<?php echo $Letra; ?>');"> <?php echo $Letra; ?>  </a>

You need to declare your function by receiving a parameter

function pBuscaFichaEmergencia(Iniciais )

And you don’t need to use jquery to get the value

  • 1

    Thanks for the help @Marco Vinicius Soares Dalalba, I managed to solve my problem.

Browser other questions tagged

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