Jump to content

Javascript Function problem


selenin

Recommended Posts

Hello, it's me again still trying to modify this script with a new problem, this is my javascript code:

 

//begin ajax grabber

 

function validateGrabber(b_on_submit){

 

    if(document.grabber.keywords.value == '' || document.grabber.keywords.value == 'search'){

 

        alert('You did not enter a search term. Please try again.');

 

        if(b_on_submit == 'true')

 

            return false;

 

    }

 

    else{

 

        document.grabber.btn.submit();

 

    }

 

}

 

function grabber(inputGrabber) {

 

if(inputGrabber.length == 0) {

 

// Hide the suggestion box.

 

$('#suggestions').hide();

 

} else if(inputGrabber.length > 2) {

 

$.post(MELODYURL2+'/ajax_grabber.php', {queryString: ""+inputGrabber+""}, function(data){

 

if(data.length >0) {

 

$('#suggestions').show();

 

$('#autoSuggestionsList').html(data);

 

}

 

});

 

}

 

} // end ajax grabber

 

and this is my template code:

 

				<form action="{$smarty.const._URL}/search.php" method="get" id="search" name="grabber" onsubmit="return validateGrabber('true');">
			<input name="keywords" type="text" value="" class="search_keywords" id="inputGrabber" {if $smarty.const._SEARCHSUGGEST == 1}onkeyup="grabber(this.value);" onblur="fill();" autocomplete="off"{/if}  />  
			<input name="btn" value="{$lang.submit_search}" type="submit" class="search_bttn" />
			<div class="suggestionsBox" id="suggestions" style="display: none;">
				<div class="suggestionList" id="autoSuggestionsList">
				</div>
			</div>
			</form>	

 

The first function doesn't work properly with this if, doesn't show alert and second doesn't work at all...

Link to comment
Share on other sites

The first function works now fine, but I have problems with the second

 

function grabber(inputGrabber) {

   if(inputGrabber.length == 0) {

      // Hide the suggestion box.

      $('#suggestions').hide();

   } else if(inputGrabber.length > 2) {

      $.post(MELODYURL2+'/ajax_grabber.php', {queryString: ""+inputGrabber+""}, function(data){

         if(data.length >0) {

            $('#suggestions').show();

            $('#autoSuggestionsList').html(data);

         }

      });

   }

}

 

it doesn't recognize #suggestions and #autoSuggestionsList

Link to comment
Share on other sites

EDIT: You already posted that you fixed the first issue, but I'm posting this anyway since it addresses some other issues in the code:

 

Show the rendered HTML - not the server-side template code.

 

However, your function seems a little illogical.

 

1. I don't see a need to pass 'true' to the function.

2. On the last line you are submitting the button? You submit the "form" not the button. but, in this case you don't need to do a submit in the javascript. The script should just return true/or false and the onsubmit trigger will behave accorddngly.

 

Change submit trigger to

onsubmit="return validateGrabber(this);"

 

Change function to this

function validateGrabber(formObj)
{
    //Trim leading/trailing spaces
    var keyword = formObj.elements['keywords'].value.replace(/^\s+|\s+$/g,'');
    if(keyword == '' || keyword == 'search')
    {
        alert('You did not enter a search term. Please try again.');
        return false;
    }
    return true;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.