1

I'm trying to pass values between two pages in jQuery mobile. I don't have any issues with any of the values except radios. With a radio on the next page I get an Uncaught ReferenceError.

Here's a simplified version of my code.

$(document).on('pageinit', '#page1', function(){

  $('#validate1').submit(function(){

    var inputVal = $('[name=inputVal]').val();
    var radioVal = $('#form1 input[type="radio"]:checked').val();
    // Also tried this
    // var radioVal = $('input[name=radioVal]:checked', '#validate1').val();

    console.log(radioVal); // Works

    $.mobile.changePage('#page2');
    return false;

  });

});

$(document).on('pageinit', '#page2', function(){

   console.log(inputVal.value); // Works
   console.log(radioVal.value); // Uncaught ReferenceError: radioVal is not defined 

});

I used this method for passing data.

Passing data between pages with jQuery Mobile?

Community
  • 1
  • 1
Ian Hoar
  • 1,174
  • 1
  • 19
  • 40
  • Both "radioVal" and "inputVal" are defined **inside** the "pageinit" handler for your first page. Thus they won't be available to the other handler. (Why "inputVal" works is not clear; it's probably redundantly declared elsewhere.) – Pointy Mar 02 '13 at 23:58
  • 1
    Removed the var keyword from radioVal. Why not just put console.log(radioVal). http://jsfiddle.net/Zq32c/101/ – Andrew Briggs Mar 03 '13 at 00:17
  • Oh I feel dumb, I wondered why my JS Fiddle was working too but not my code. Removing var worked, I forgot about variable scope. If you post this as an answer I'll give you the credit. Thanks. – Ian Hoar Mar 03 '13 at 00:29

1 Answers1

0

Initialize

 var radioVal = "";

outside "pageinit" (perhaps at the top of your JS file)

and in pageinit simply put

radioVal = $('#form1 input[type="radio"]:checked').val();

in this way, you will treat radioVal as a global variable and make it available anywhere.

gcatalfamo
  • 243
  • 2
  • 14