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 8252915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:41:44+00:00 2026-06-08T00:41:44+00:00

I don’t understand why the this-reference in function foo is not set with #searchval,

  • 0

I don’t understand why the “this-reference” in function foo
is not set with #searchval, when called by load event ?

The function call of foo at the keyup event works but the call does not work at the load event cause this-reference is not set.

Example:

<body>
<form action="" method="post">
<input type="text" name="test" value="<?=$_POST['test']?>" id="searchval">
<select id="names" size="3">
<option value="1">WTF1</option>
<option value="2">WTF2</option>
<option value="3">WTF3</option>
</select>
<input type="submit">
</form>
</body>
<script type="text/javascript">

$(document).ready(function() {
    var names = $('#names option').clone();

    function foo() {
        //Change the code to $('$searchval').val(); make it work
        var val = $(this).val(); 
        $('#names').empty();
        names.filter(function(idx, el) {
            return val === '' || $(el).text().indexOf(val) >= 0;
        }).appendTo('#names');
    }
    $('#searchval').bind('keyup', foo);
    $('#searchval').load(foo());
});
</script>
</html>
  • 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-08T00:41:47+00:00Added an answer on June 8, 2026 at 12:41 am

    You’re setting up the handler with the return value from “foo”, not the function itself.

    It should be:

    $('#searchval').load( foo );
    

    When you pass foo(), you’re first invoking the function — that’s what ( ) does. By passing just the function reference you get the effect you want.

    edit — on top of that issue, as noted in a comment you’re trying to set up a “load” handler for an element that’ll never get such an event. What is it that you expect to cause that event? Perhaps “change” is what you want instead of “load”.

    edit again — if you just want to simulate having “foo” called as it is when a “keyup” event happens, you can do two things:

    1. You can use “triggerHandler”:

      $('#searchval').triggerHandler('keyup');
      
    2. You can use “call”:

      foo.call($('#searchval')[0]);
      

    In both cases, of course, you won’t get an “event” object that’s like the one you get on a real event, but “foo” doesn’t care about that anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't quite understand why this copy constructor is not invoked when I build with
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
I don't know if this question is trivial or not. But after a couple
I don't know why this double json response are not successful: [{first_content:content,...}][{second_content:content,...}] So i
Don't understand, if Data.Map is and [] is. I found this out while wondering
Don't want to sort the entries. using this does not preserve the order as
Don't understand why #include <Header.h> is not compiling while #include Header.h is compiling with
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
I don't know why, but this code worked for me a month ago... maybe
(Don't know if this is strictly on-topic, but I don't see any better Stack

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.