1

I have a HTML file:

<!DOCTYPE html>
<html>
<head>
<title>Insert</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
<script type="text/javascript" src="js/jquery-1.10.1.js"></script>
<script type="text/javascript" src="js/controlla.js"></script>
</head>
<body>
<div id="insert">
<img src="css/images/logo.png" alt="Search Engine Logo" />
<input id="#textboxone" onkeyup="scova(event)" type="text"/>
</div>
</body>
</html>

I have inserted script reference in order (first jquery, second my function file). In my function file, there is function scova(event) from onkeyup event in inputbox, is not possible use jQuery reference $.

For example:

function scova(e) {
var valore = document.getElementById('#textboxone').value;
var valjquiro = $('#textboxone').val();
console.log('il valore di javascript: ' + valore);
console.log('il valore di jaquirro: '+valjquiro);
};

And in the browser console (after the digit 'test') I get:

il valore di javascript: test
il valore di jaquirro: undefined 

How can I use JQuery in my function file? How to use $ reference of jquery in my controlla.js?

1
  • Recheck the <input id="#textboxone"... the Number sign (#) might be the issue it either should be removed or escapped i think
    – ErickBest
    Jun 14, 2013 at 16:41

4 Answers 4

8

Change

<input id="#textboxone" onkeyup="scova(event)" type="text"/>

to

<input id="textboxone" onkeyup="scova(event)" type="text"/>

so that $('#textboxone') won't be an empty object.

Once you've done that, fix also document.getElementById('#textboxone') by removing the #.

6
  • 1
    Hate when I miss the little things... +1
    – tymeJV
    Jun 14, 2013 at 16:35
  • I should've not answered the written question -_-' +1 for fixing the issue instead answering the useless question.
    – Jeff Noel
    Jun 14, 2013 at 16:39
  • @Ghillied I'd rather say that before answering the title of the question you should have looked a little more at its content. Jun 14, 2013 at 16:41
  • 1
    He'll also need to remove the '#' from the getByElementId Jun 14, 2013 at 16:41
  • @dystroy Usualy when people don't know how to formulate their needs I don't bother answer (that's how we do it in the company I work for), so I try sticking to what people ask. I know it's not the good way to do it though.
    – Jeff Noel
    Jun 14, 2013 at 16:59
2

either remove # from your id attribute OR do this -

var valjquiro = $('#\\#textboxone').val();
1

You don't need to include jQuery inside any other JavaScript file: just put jQuery on top of all your <script> tags and all the next files imported will have access to the library.

0

Try wrapping up the code from your function file like this:

$(document).ready(function(){ put your code here })

Read more about this here: http://api.jquery.com/ready/

Your code is probably running before the DOM has fully loaded, in its current state, if I understood you correctly.

1
  • 1
    No, that can't be the problem as the function is called from an event handler. Jun 14, 2013 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.