Question

Consider the below html code snippet-

I'm trying to pass a js variable pages to show_data_per_page function,but I'm getting a syntax error.I also tried the following-

<input type='text' name='search' onkeyup='show_data_per_page(pages);'/>

or

<input type='text' name='search' onkeyup='show_data_per_page("pages");'/>

or

<input type='text' name='search' onkeyup='show_data_per_page('+pages+');'/>

But nothing seems to work.The eclipse editor refuses to recognize pages as a js variable.

Please help.

Was it helpful?

Solution

Try this out:- http://jsfiddle.net/adiioo7/bNBdX/1/

Working fine for me.

HTML:-

<input type='text' name='search' onkeyup='show_data_per_page(pages);'/>

JS:-

var pages=10;

function show_data_per_page(obj)
{
    alert(obj);
}

OTHER TIPS

This worked for me: This is a part of code to copy from one textbox to another. I tried your requirement of passing a variable to a function via onkeyup. "pages" here will be the variable. Please make changes based on your needs.

js:

<script type="text/javascript">
        function copy_data(val,pages) {
            var a = document.getElementById(val.id).value+pages
            document.getElementById("copy_to").value = a
        }
</script>

HTML:

From:<input type="text" name ="a" id="copy_from" @onkeyup= "copy_data(this,"+pages+")"/><br>
To:<input type="text" name ="b" id="copy_to"/><br>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top