Is it possible to use a Javascript-enabled slider?

Asked

Viewed 53 times

0

would like to share variables between functions in javascript, it is possible?

function 1:

$('#btnCompra').click (function(){
 localStorage.tituloProd = 'arroz';
 tituloProd = JSON.stringify(localStorage.getItem('tituloProd'));
 });
 });

function 2:

$(document).ready(function(tituloProd){
  $('.descricaoProduto').text(tituloProd);
});

FUNCTION JS COMPLETE:

var tituloProd = '?';
 $(document).ready(function(){

    $('#btnCompra').click (function(){

      localStorage.tituloProd = 'arroz';
      tituloProd = JSON.stringify(localStorage.getItem('tituloProd'));
    });

    $('#carrinho').click (function(){
      $('.descricaoProduto').text(tituloProd);
    });
    });
  • Weslley, if the functions are in the same source, you can declare the variable outside the functions, this will cause both to access the same variable.

  • Ola Daniel! I did this, but because I was using "ready Function" in both functions, the value of the variable in the second function remained "undenified", since I declared it empty and only assigns within the function. Is there any method I can use in the "ready Function ludar"?

  • I edited there, as you can see, my variable 'tituloProd' was started with '?'. In the first method it receives a stringfy, and it is this value that I would like to pass to the second function, but the value that appears is the '?' that has been declared.

  • Weslley, I saw you asked another question, in fact this routine suffers a refesh? Would pass this value between pages?

  • What I really want is that when my customer clicks on "add cart" the information like product name, price, image and etc, is displayed on the "cart" page. But I didn’t want to use database for this, I wanted to pass the variables through the pages with javascript. But when I use localStorage, it is limited to the function that created it, I cannot send it to another function

  • and I’m not sure about the refresh, what I was hoping would happen is that when the customer clicked on "add cart" the information would already appear on the other page

  • I usually see the sites using sessionStorage for that part of cart

  • The question you asked has no relation to what you are looking for. In fact the second edition already solves the problem proposed in the question. What you’re looking for is this here

  • It doesn’t solve the problem, so I kept the topic open. In the first function the variable receives a value, and in the second function the value that appears is what was declared ('?'), this is the problem I am facing, the value assigned in the first function does not appear in the second function.

  • About the page: I had read before and did not solve my problem, unfortunately

Show 5 more comments
No answers

Browser other questions tagged

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