1

I have two buttons that are meant to activate my JavaScript functions when clicked, but they don't seem to work. However, when I move my JavaScript function content outside the functions, they work correctly. So the buttons are definitely the problem.

JavaScript:

$(document).ready(function() 
{

function recordjourney() 
{
var journey = JSON.parse(localStorage.getItem('journey'))||[];
journey.push(location.protocol + '//' + location.host + location.pathname);

localStorage.setItem('journey', JSON.stringify(journey));

document.write(journey);
}


function resetjourney() 
{
localStorage.clear()
}

});

HTML:

<p><button name="record" type="button" onclick="recordjourney()">Record Journey</button</p>

<p><button name="reset" type="button" onclick="resetjourney()">Reset Journey</button></p>
3
  • 2
    put both functions outside $(document).ready(function().... Commented Apr 15, 2012 at 14:10
  • You know that document.write(journey); will replace the whole document? Commented Apr 15, 2012 at 14:13
  • @Felix Kling Yeah, I just had it there to see if the buttons work. Commented Apr 15, 2012 at 14:21

3 Answers 3

3

The buttons aren't the problem, you have a scope issue since the functions you are calling don't exist on the same level as the buttons.

You can fix that and make your code a bit cleaner by binding to your buttons inside the ready call like so

$(document).ready(function() {
   $('[name="record"]').click(recordjourney);
   $('[name="reset"]').click(resetjourney);    

});

function recordjourney() {
    var journey = JSON.parse(localStorage.getItem('journey')) || [];
    journey.push(location.protocol + '//' + location.host + location.pathname);

    localStorage.setItem('journey', JSON.stringify(journey));

    document.write(journey);
}


function resetjourney() {
    localStorage.clear()
}​

<p><button name="record" type="button">Record Journey</button</p>

<p><button name="reset" type="button">Reset Journey</button></p>​

Fiddle here - http://jsfiddle.net/7eYNn/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks... helped me solve a problem. This is actually an important concept for new JavaScript programmers. And easy to get into trouble with it at any time.
0

initialize your functions out of $(document).ready().

$(document).ready(function() 
{

});


function resetjourney() 
{
   localStorage.clear()
}


function recordjourney() 
{
  var journey = JSON.parse(localStorage.getItem('journey'))||[];
  journey.push(location.protocol + '//' + location.host + location.pathname);

  localStorage.setItem('journey', JSON.stringify(journey));

  document.write(journey);
}

Comments

0

Yeah, that's right. If you define a function inside a function, it will be private to that function. You need to create a global var

var recordjourney;
$(document).ready(function(){
 ...
recordjourney = {
  var journey =
 ... etc

Although, of course, given that you are using JQuery I'd do

$(document).ready(function(){
 ...
    $( 'button[name=record]' ).bind( function(){ 
       //put your function here 
    })

and remove the ugly onclick="recordjourney from the button tags.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.