views:

35

answers:

5

Trying to clean up some code. I have a button that has the following inline JS

<input id="lookup" type="submit" name="lookup" value="Search" onclick="changestart('1')" />

I would like to change the onclick to a jQuery click funnction but am getting no success. Here is the "changestart" function

function changestart(direction) {
    var rowsElement  = $("#maxrows");
    var rowsValue    = parseInt(rowsElement.val());
    var startElement = $("#startID");
    var value        = parseInt(startElement.val());
    startElement.val(direction == "forward" ? value + rowsValue : direction == "back" ? value - rowsValue : 1);
}

$("#previous").click(function(){changestart('back');});
$("#next").click(function(){changestart('forward');});

I've tried

$("#lookup").click(function(){changestart.val(1);});

but it doesn't work.

+1  A: 

Don't you just mean:

$("#lookup").click(function(){changestart(1);});
Pim Jager
@Pim Jager and @JamesStuddart - thanks guys, this works. I did not know that when setting value for a function you don't need .val(). I have a lot to learn, thanks for your help
Dirty Bird Design
+1  A: 
$("#lookup").click(function(){ changestart(1); });
mkoistinen
@mkoistinen - thank you. so when you are setting a value for a function you do not use .val? Learned something, thank you sir!
Dirty Bird Design
mkoistinen
+1  A: 

Ensure the javascript is either under the button in the markup OR use:

$(document).ready(function(){
    $("#lookup").click(function(){changestart(1);});
});
JamesStuddart
+2  A: 

Since your code defaults down to 1 anyway, you can just do this:

$("#lookup").click(changestart);

There's no .val() method on functions, and the argument you're passing doesn't matter anyway, anything except "forward" and "back" are 1, so you can pass with no argument as well, which is what the above code does.

Nick Craver
@Nick Craver - so the other answers also work, but yours is even shorter. Awesome. Thanks Nick!
Dirty Bird Design
@Dirty - welcome :)
Nick Craver
A: 

Try this

<input id="lookup" type="submit" name="lookup" value="Search" onclick="changestart('1')" />

to

<input id="lookup" type="submit" name="lookup" value="Search"/>

and

$("#lookup").click(function(){changestart.val(1);});

to

$("#lookup").click(function(){changestart(1);});
JapanPro