Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8214675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:32:10+00:00 2026-06-07T11:32:10+00:00

I am confused on how the .val() method in jQuery handles whitespace (if it

  • 0

I am confused on how the .val() method in jQuery handles whitespace (if it is a jQuery problem at all).

My example is using an AJAX suggestion box feature which I’m slowly developing. It uses Codeigniter as the framework.

I have this jQuery code:

$(document).ready(function(){
    $("#suggestbox").hide();
    $("#searchboxinput").live("keyup", function(){
        if ($("#searchboxinput").val().length==0){
            $("#suggestbox").fadeOut(100);
        }else{
            $("#suggestbox").fadeIn(100);
            $("#sugresultcontain").load('http://localhost/ajaxtest/ajaxhandle/suggest/'+$("#searchboxinput").val()+' #loadsuggest');
        };
    });
});

The ‘#suggestboxinput’ is a text input as shown:

<div id="content" class="clear">
    <div id="searchwrap" class="clear">
        <div id="searchbox" class="float-left"><input id="searchboxinput" name="" value="" type="text"></div>
        <div id="searchbutton" class="float-right"><input id="searchbuttoninput" name="" type="button" value="Go"></div>
    </div>
    <div id="suggestbox" class="clear">
        <div id="suggesthead">Suggestions:</div>
        <div id="sugresultcontain"></div>
    </div>
</div>

I then have (Using code igniter) a method which handles the .load() as shown:

public function suggest($q=''){

        $suggest = $this->ajax_handle->suggest();

        for($i=0; $i<count($suggest); $i++){
            $db[$i] = $suggest[$i]['firstname'].' '.$suggest[$i]['lastname'];
        }

        if(strlen($q)>0){
            $sugstr = '';
            $e = 0;

            for($f=0; $f<count($db); $f++){
                if (strtolower($q)==strtolower(substr($db[$f],0,strlen($q)))){
                    $sugresult[$e] = $db[$f];
                    $e++;
                }
            }

            if(isset($sugresult)){
                $data['sugresult'] = $sugresult;
                $this->load->view('ajaxref/suggest', $data);
            }else{
                $this->load->view('ajaxref/suggest');
            }
        }else{
            $this->load->view('ajaxref/suggest');
        }
    }

This works almost as intended, if I load the page then use the text input the names from the database do show (according to text field). It works fine if I type the first name and will continue to show no matter how many spaces I put after the field (which is perfect).

However, the problem lies with typing the surname even after just a single space (Dominic Sore) or even just the first letter or the last name (Dominic S). No names show in the suggestion box.

I had read that using a ‘+’ symbol in the input box will work. I tested it, and it did work (Dominic+Sore outputs Dominic Sore in suggestion box). Understandably though, I cannot have users type a ‘+’ for every space they want to use.

So I am just wondering how do I handle this? I’ve been reading a lot but I cannot find a solution.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:32:11+00:00Added an answer on June 7, 2026 at 11:32 am

    try $.trim method:

    Remove the whitespace from the beginning and end of a string.

    var txt = $.trim($("#searchboxinput").val()).replace(' ', '+')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This section has me thoroughly confused. I have an example problem that I am
First time using jQuery parent and got confused, here's my html: <div id=friends_inveni class=friends_rec
I am trying to post data using ajax in jquery of json datatype. I
As a jQuery neophyte I am somewhat confused by the different contexts in which
Confused here... I have two dates which I NSLog to console and get: dates
Really confused why the QR output using RcppArmadillo is different than QR output from
I am a little confused here. What should I use Console.WriteLine((val/1085).ToString(N)); VS Console.WriteLine(String.Format({0:N}, (val/1085)));
I was doing some self-learning on how to pass data from JQuery Ajax to
Using jquery1.7.1 and django1.3 ,I was trying to make a post request through ajax,in
What is happening in below jQuery code ? $(#myDiv).val($(.cssValue).sortable('serialize',{expression: 'textExp[#]'})); I understand its enabling

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.