0

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
  • what is it that you are trying to accomplish with the button ? Commented Sep 20, 2010 at 13:21

5 Answers 5

2

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.

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

1 Comment

so the other answers also work, but yours is even shorter. Awesome. Thanks Nick!
1

Don't you just mean:

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

1 Comment

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
1
$("#lookup").click(function(){ changestart(1); });

2 Comments

thank you. so when you are setting a value for a function you do not use .val? Learned something, thank you sir!
Nope. val() is used for getting/setting the value of a form element. For example, $('form select').val() would provide the value of the option that the select element is currently set to. Similarly, $('form select').val(5) would set it to the option with a value of 5. Also works with other form elements like checkboxes, radio buttons, input fields & textfields (did I leave any out?). Glad I could help!
1

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

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

Comments

0

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);});

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.