1

I have this code:

    jQuery(function( $ ){

    // If no speed is set (when loading the page) set default speed

    if (!$moveSpeed) {
        var $moveSpeed = 4000;
    }

    // Move the damn autocue

 $('a.goDown').click(function(){
        alert($moveSpeed);
  $.scrollTo('#footer', $moveSpeed);
  return false;
 });

    $('a.goUp').click(function(){
        alert($moveSpeed);
  $.scrollTo('#header', $moveSpeed);
  return false;
 });


    // Speed settings for autocue

    $('a.changeSpeed').click(function(){
  $moveSpeed = $(this).attr('speed');
  return false;
 });
});

</script>

and if the changeSpeed is clicked, the alert box shows it is changed to the given number but it aint an integer, is there a way to convert a variable in to an integer?

thx :)

1
  • 2
    If you're going to put custom attributes on elements, strongly recommend using a data- prefix on them (e.g., data-speed). Custom attributes are invalid in HTML4 and earlier; in HTML5, they're valid if they start with data-. Browsers generally allow invalid attributes and so they work, but they make a document fail validation. More in this article Commented Dec 21, 2010 at 11:20

2 Answers 2

10
$('a.changeSpeed').click(function(){
  $moveSpeed = parseInt($(this).attr('speed'),10);
  return false;
 });

Javascript parseInt Function

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

3 Comments

+1 Nice use of parseInt, but it would be even nicer if you used this.speed rather than the unnecessary jQuery call!
@lonesomeday - what makes you think the current this has a property speed? I assumed it was an html attribute in which case the jQuery call is legit.
@lonesomeday: Not all attributes are reflected as properties. I certainly wouldn't count on a non-standard and invalid one being reflected.
1

I don't think it's the actual problem you're having, so I'm making this answer a CW, but FYI:

jQuery(function( $ ){

    // If no speed is set (when loading the page) set default speed

    if (!$moveSpeed) {
        var $moveSpeed = 4000;
    }

That if statement is completely non-functional, the body of it is always run. The !$moveSpeed condition will always be true. This is because var declares a variable within the current function, regardless of where the var statement actually is or whether it's inside a condition or loop. More details in this article, but basically the interpreter sees that code like this:

jQuery(function( $ ){
    var $moveSpeed;

    // If no speed is set (when loading the page) set default speed

    if (!$moveSpeed) {
        $moveSpeed = 4000;
    }

...and since the value of a variable that isn't initialized yet is undefined, and !undefined is true, you'll always set $moveSpeed to 4000.

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.