views:

229

answers:

4

Hi all,

I am very new to Javascript and have only started playing with jquery recently. I have a Javascript function that while used on its own within my project, works flawlessly. as below.. * UPDATE * these 2 functions are declared after the <input> tag and still throw the same error document.getElementById("name_first") is null when i add the jquery code.

<script type="text/javascript">

function printSymbol(symbol) {  document.getElementById('name_first').value
+= symbol; };

function deleteSymbol() {  document.getElementById('name_first').value
= document.getElementById('name_first').value.substr(0, document.getElementById('name_first').value.length
- 1); };

</script>

However, when i add my jquery to the same element my existing javascript function returns the error : document.getElementById("name_first") is null

The Jquery function works fine and is declared as below.

<script type="text/javascript">
$(document).ready(function() {

$("input[type=text]").autoSuggest("search_data.php", {selectedItemProp: "name", searchObjProps: "name", minChars: 2});

});
</script>

:::::UPDATE::::::

I call the printSymbol function from a flash keyboard with the following Actionscript code, which works fine.

getURL("javascript:printSymbol('" + evnt.data + "');");

I have tried as suggested earlier to do this using only jquery functions as below.

<script type="text/javascript">
$(document).ready(function() {

$("input[type=text]").autoSuggest("search_data.php", {selectedItemProp: "name", searchObjProps: "name", minChars: 2});

function printSymbol(symbol) {
    $('name_first').append(symbol)
};

function deleteSymbol() {
    $('name_first').text( $('name_first').text().substr(0, $('#name_first').text().length - 1))
};
});
</script>

However i do not know if this is working as i cant call these functions with my current Flash keyboard that is using : getURL("javascript:printSymbol('" + evnt.data + "');"); to call. How would i externally call the new jquery functions?

Hope this is a little more informative for u guys, and thanks very much for your help so far :).

UPDATE

Please see below the fully updated code with some updates ( including html )

<?php session_start(); ?>
<?php require_once('Connections/db.php'); ?>


          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
          <html>
          <head>
          <title>None</title>
          <!-- Include Javascript functions and css files -->
          <link href="autoSuggest.css" rel="stylesheet" type="text/css" />
          <script type="text/javascript" src="jquery.js"</script>
          <script type="text/javascript" src="jquery.autoSuggest.js"</script>

            <script type="text/javascript">
            $(document).ready(function() {
            $("#name_first").autoSuggest("search_data.php", {selectedItemProp: "name", searchObjProps: "name", minChars: 2});
             });
            </script>

          </head>

          <body>

          <form action="" method="get" name="form1" target="_self" id="form1">
          <input name="name_first" type="text" id="name_first" value="" size="25" maxlength="50" />
          </form>

<script type="text/javascript"> 

function printSymbol(symbol) {  
$("#name_first").val($("#name_first").val() + symbol); 
} 

function deleteSymbol() {  
$("#name_first").val($("#name_first").val().substr(0,$("#name_first").val().length 
- 1)); 
} 

</script>


     <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="582" height="241">
    <param name="movie" value="images/KB_NoNum.swf" />
    <param name="quality" value="high" />
      <embed src="media/KB_With_Space.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="582" height="241">  </embed>
    </object>
    </body> 
    </html>

NEW UPDATE USING AUTOCOMPLETE FROM BASSISTANCE.

Code im now using with autcomplete plugin ( NOT ) autosuggest anymore.

  <script>
  $().ready(function() {
    $("#name_first").change().autocomplete("data_search.php", {
        width: 260,
        selectFirst: false
    });
   });

  </script>

<script type="text/javascript"> 

function printSymbol(symbol) {  
$("#name_first").val($("#name_first").val() + symbol); 
} 

function deleteSymbol() {  
$("#name_first").val($("#name_first").val().substr(0,$("#name_first").val().length 
- 1)); 
} 

</script>

The function of the auto complete works fine pulling data from database etc while using normal keyboard.

When using my on screen keyboard that uses the printSymbol function, the text is entered into the text field, BUT it does not trigger the autocomplete.... confused

It seems like this autocomplete plugin must use the keypress function to operate, so stumped again i think.

UPDATE OF jQuery function ( not working )

    <script>
  $(document).ready(function() {
   $('.selector').bind('autocompletechange', function(event, ui) {
    $("#name_first").change().autocomplete("data_search.php", {
        width: 260,
        selectFirst: false
    });
   });
   });

  </script>

UPDATE using http://jquery-ui.googlecode.com/svn/tags/1.8rc1/ui/jquery-ui.js

Code used

    <script>
  $(document).ready(function() {
    $("input").autocomplete({
     source: "search_data.php"});

   });
  </script>

Again the function works with normal keyboard, but the input from the on screen keyboard does not trigger :( using keypress i guess again.

+2  A: 

Are you sure that you haven't changed your form? Perhaps you removed the id from your input? It should be:

<input type="text" name="name_first" id="name_first" />

The error suggests that either the id attribute is now missing, or is no longer "name_first".

The jQuery works fine because it is matching all <input> tags with type="text".

So, <input type="text" name="name_first" /> matches your jQuery selector, but not your document.getElementById('name_first') selector.

============================================

Edit following updates

Looks like the autosuggest only runs on the onKeyDown event, which isn't ever being called since you are changing the value, not typing into the input.

I once had to get an autocomplete box working with an on screen javascript keyboard and I ended up having to manually call the autocomplete on each key "press". I don't have access to the code anymore sorry (don't work there anymore).

What you'll need to either do is find an autocomplete that works correctly with values being changed by javascript (unlikely), or work out how to call the autocomplete manually.

To do this, start off by giving the input field a default value (to save time) and start trying to work out (using the firebug console, which you can type javascript commands into) what you need to send to get the autocomplete to fire. Start by putting in keyChange() and hitting enter (that's the function name that is inside autocomplete). See if anything happens. Someone else might be able to comment on whether (or how) you can call jQuery extension functions externally (google might know too).

As for why your <input> tag isn't being updated, add some console.log() calls inside your printSymbol function. Eg:

function printSymbol(symbol) {
    console.log('symbol received: ' + symbol);
    console.log('name_first.val (pre): ' + $("#name_first").val());
    $("#name_first").val($("#name_first").val() + symbol); 
    console.log('name_first.val (post): ' + $("#name_first").val());
} 

Should help you work out what's going on. If they all look correct, then autocomplete is probably resetting the value again.

Blair McMillan
Hi, i thought this myself but to no avail the id is well and truly set :(<input name="name_first" type="text" class="centertext" id="name_first" value="" size="25" maxlength="50" />
Matthew
Oh dear lol. Well thanks for all your advice, i shall go away and mess till my brain explodes :)Thanks again Blair.
Matthew
A: 

are you using document.ready ?? if not wrap your jquery code with this ocde

$(document).ready(function() {

.... Jquery code goes here 


});

EDIT

function printSymbol(symbol) {
    $('#name_first').append(symbol);
}

function deleteSymbol() {
    $('#name_first').text( $('name_first').text().substr(0, $('#name_first').text().length - 1);
}
From.ME.to.YOU
Hi, Yes, as per the code in initial post the Jquery code is wrapped within the document ready function, and the jquery function itself is working. Its just now my normal javascript function does not work anymore, this is not wrapped within the jquery doc ready func.
Matthew
try to use jquery for your functions, CHECK MY ANSWER EDIT !!
From.ME.to.YOU
Is the normal javascript function in the <head> tag? If it is, and it's not within the jQuery document ready function, then you are trying to access the `<input>` tag before it actually exists (the DOM hasn't loaded). Either put the normal javascript within a document ready function (either the jQuery one, or google for alternatives), or move the normal javascript `<script>` block to **after** where the `<input>` tag is. Or as From.ME.to.YOU has added, convert your normal javascript to jQuery equivalents (and put inside the document ready).
Blair McMillan
ya i think that is why ... is i asked him to use jquery which will not have HEAD problem
From.ME.to.YOU
jQuery will still have the problem if it's being called before the DOM exists.
Blair McMillan
Seems like theres some progress here guys, i am now getting the following error : printSymbol is not defined javascript:printSymbol('Q'); ( for example ) the same applies to the deleteSymbol. I have gone with converting to jquery equivalents as ME TO YOU's edit. Any ideas?
Matthew
Blair, i did previously put my standard javascript after the <input> tag, still did'nt work when the jquery was added.
Matthew
Just a thought, how do i go about calling this function externally, currently i am using : getURL("javascript:printSymbol('" + evnt.data + "');"); from a flash Actionscript object. I'm guessing i need to change this to call the new jquery version of the function?
Matthew
Where is printSymbol being called (what are the lines of code that have printSymbol in them - update your question with this info as it's easier to format)? Things you should confirm: 1. Where printSymbol/deleteSymbol is being called is either within jQuery document ready or after the <input> tag. 2. That the printSymbol/deleteSymbol functions are outside of the jQuery document ready function.
Blair McMillan
Updated original question as per your request. Thankyou.
Matthew
@Matthew have a look at this http://docs.jquery.com/UI/Autocomplete and take a look at my updated answer
c0mrade
@c0mrade yes i did this, still issues as per original question update.
Matthew
+2  A: 

You should put your functions/methods before or after $(document).ready(function() not inside however autosuggest you should put inside

Did you try like this together with jquery:

<script type="text/javascript"> 

function printSymbol(symbol) {  
$("#name_first").val($("#name_first").val() + symbol); 
} 

function deleteSymbol() {  
$("#name_first").val($("#name_first").val().substr(0,$("#name_first").val().length 
- 1)); 
} 

</script>

Since you switched to autocomplete I might be able to help you, you need not to alter the jquery plugin itself, you can check out the demos example , look at the page source ex:

$("#suggest1").focus().autocomplete(cities);

this will autocomplete input with id suggest1 with array cities defined somewhere in the page on focus, you can change this to change if you have static data. The data on this plugins demo page is called localdata.js take a look at it.

However if you have dynamic data you can use this :

$("#singleBirdRemote").change().autocomplete("search.php", {
        width: 260,
        selectFirst: false
    });

or you can use focus or whatever .. my timing is short I need to go ..hope this helps

Try this :

 <script>
  $(document).ready(function() {
   $('.selector').bind('autocompletechange', function(event, ui) {
  ...
   });
   });

  </script>
c0mrade
Hi C0mrade, yes this is how it was, but not working. The autosuggest within the doc ready function, while the other standard javascript functions were after the <input> tag, however as soon as the autosuggest was added, my original function cease to work.
Matthew
@Matthew what exactly are you passing to this function printSymbol, which argument, can you post simple input simple output ? Does autosuggest work without these functions?
c0mrade
Hi yes, autosuggest is working. I am using an onscreen Flash keyboard that i have had in use for years, it uses the following function to call the printSymbol : getURL("javascript:printSymbol('" + evnt.data + "');"); which works perfectly well unless the jquery is added to the page. The Onscreen keyboard calls the printSymbol function which enters key presses to the text field in question.As from one of the answers below, i attempted to use jquery functions to achieve this as suggested, but now my printSymbol function as called previously does not exist, so from one problem to another :).
Matthew
@Matthew .. so when you comment out the whole jquery including autosuggest and document ready function do you still get this error : `document.getElementById("name_first") is null`
c0mrade
No, if i remove the jquery it works perfectly :(
Matthew
I have just tried the above code, i am now getting no errors reported in firebug, however the text is not been inputted into the name_first text field.
Matthew
@Matthew ok, so what you want to accomplish , can I have simple input.. and simple output .. I really don't understand .. I tried ..
c0mrade
@Matthew my bad Matt try my edited answer
c0mrade
c0mrade, I am trying to get the following result >>For the jQuery autoSuggest function to work for text field `name_first` WHILE inputting data with my onscreen keyboard using the printSymbol and deleteSymbol function.If i just have the jQuery function, and input data with normal keyboard it works just fine. but this is for a touchscreen app so the onscreen keyboard is imperative.If i remove the jQuery autosuggest function from my page, the printSymbol and deleteSymbol function work as expected and data is entered into the `name_first` text field.
Matthew
I am trying to get both functions to work together, as of yet they dont. Does this help?
Matthew
@Matthew I updated my answer
c0mrade
i c0mrade, i have got the jQuery autocomplete working just fine.My normal javascript inputs text into the text field now, but it doesnt trigger the autocomplete. If i use normal keyboard it triggers, with the on screen keyboard using my javascript functions it doesnt trigger, ive tried changing to onChange and onFocus. Any ideas? :)
Matthew
onChange and OnFocus is wrong syntax .. this is right `$("#singleBirdRemote").change().autocomplete("search.php", { width: 260, selectFirst: false });`or this `$("#singleBirdRemote").focus().autocomplete("search.php", { width: 260, selectFirst: false });`
c0mrade
hi c0mrade i have done as suggested, and posted my code at end of original question. Still the printSymbol does not trigger the autcomplete function but text IS entered into text field when typed.... *confused*
Matthew
Ok i thnk i might be been a bit dumb here, i tried the code you gave where the ... is i put in the following.` $("#name_first").change().autocomplete("data_search.php", { width: 260, selectFirst: false });`this doesnt work at all, guess i have done something wrong?
Matthew
nah you did as I wrote have you checked for Try this : latest update and the jquery api http://docs.jquery.com/UI/Autocomplete
c0mrade
I have just tried this, ive updated original question.
Matthew
A: 

I have seen weird issues with this when the functions did not have terminating semi-colons such as:

<script type="text/javascript"> 

function printSymbol(symbol) {  document.getElementById('name_first').value 
+= symbol; }; 

function deleteSymbol() {  document.getElementById('name_first').value 
= document.getElementById('name_first').value.substr(0, document.getElementById('name_first').value.length 
- 1); }; 

</script>
Mark Schultheiss
Thanks for your suggestion, but im afraid no joy :(
Matthew