Send variable via Jquery function

Asked

Viewed 245 times

1

I’m learning Jquery and getting my ass kicked.

I created a function in the js file like this:

$(function abrir_qd1(variavel){
$(this).click(function(){
            $("#quadro1").load('arquivo.php?v=' + variavel); //carrega conteudo
});

});

and the function is called via an html link:

<a onclick="abrir_qd1('var_enviada')" class="list_nome_aguard">click aqui</a>

And through an Alert I discovered that the problem that the variable arrives at the function so:

arquivo.php?v=function (a,b){return new p.fn.init(a,b,c)}

I can’t handle it here.

Thank you in advance

1 answer

0


There are some patterns together there.... the first you will use when you want the function to be called in the page load, no action from anyone, then Voce does so:

$(function(){
    alert('carregou');
});

For Voce to declare a function, Voce only declares the name and variable, so it waits to be 'called' by some action, in this case you declare it in the onclick of the link like this:

function abrir_qd1(variavel){
       $("#quadro1").load('arquivo.php?v=' + variavel); 
}

There is also the possibility of you using jquery to have a selector and an action and every time it finds the selector and the action it executes, without Voce need to use the onclick action for example.

$(".algumaClasse").click(function(){
      $("#quadro1").load('arquivo.php?v=' + variavel); 
});

If you put the 3 together it won’t go anywhere as it did there...

  • The function is working. the problem is when it receives the variable there in the function 'open_qd1(variable){' It arrives at the function like this: 'Function (a,b){Return new p.fn.init(a,b,c)}' It transforms the variable

  • as he said above this mess, put an ID in your tag "<a>" and call it as in the last form he demonstrated in the example.

  • Got it. Problem solved. Thank you very much to the staff, I was putting the function in wrong place and so I did all that mixing.

Browser other questions tagged

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