0

Given this html...

<input id="txtbox-page" maxlength="4" />

how can I create a button or a link that executes a javascript function, where its parameter is the input box value?

I have to call a function, that is already in production, that has this format:

javascript:pageClient('?p=2');

where the number 2, has to be the value of the input box.

1
  • How about you wrap pageClient with another function that reads input box value and call pageClient? Commented May 8, 2014 at 16:14

3 Answers 3

1

HTML

<button id="pageClientSubmitter">Click me</button>

JS

var pageClientSubmitter = document.getElementById('pageClientSubmitter');

pageClientSubmitter.onclick = function () {
  var val = document.getElementById('txtbox-page').value;
  pageClient('?p=' + val);
}

Demo

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

Comments

1

HTML:

<a id="mylink" href="foo.bar">Link</a>

JS:

var box = document.getElementById('txtbox-page');
document.getElementById('mylink').addEventListener('click', function(){
    // Use `box` here
    pageClient('?p=' + +box.value);
}, false);

The second + is optional. I used it to convert to number in order to avoid injection attacks.

Comments

1

You could do something along these lines:

function GetTxtBoxValue() {
    var txtBoxPage = document.getElementByID("txtbox-page");
    pageClient('?p='+txtBoxPage.value);
}

Then bind that function to your form submission.

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.