Skip to content Skip to sidebar Skip to footer

Asp Textbox Calls Javascript Function

I have a search textbox in asp. And I want it to send request to the server each time the text is changed there. I have a javascript function which sends request but it is not bein

Solution 1:

You should use onKeyPress event to call the function.

<asp:TextBoxID="search"name="Search"runat="server"onKeyPress="javascript:text_changed();"></asp:TextBox>

Solution 2:

Shivam's answer is right. You can use KeyPress event to get users key strokes with that event.

But i want to inform you, you should not use ASP.NET control ids like that :

document.getElementById("ContentPlaceHolder1_search").value;

Because you'll get errors if you put your textbox somewhere else in html hierarchy, ASP.NET regenerates it's id.

Try that one, instead :

function text_changed(textObj) {
     searchedword = textObj.value;
     SendRequest();
}

<asp:TextBox ID="search" name="Search" runat="server" 
  onKeyPress="javascript:text_changed(this);"></asp:TextBox>

Solution 3:

The functionality you asking can achieve by

Use onkeyup or onkeydown instead.

This will then run the function when you type or click on the textbox. You can also then detect the keycode of the event, and prevent the function if you dont want it to run for certain keys.

Use the below code

    $("#search").keydown(function(){
    text_changed();
  });
  $("#search").keyup(function(){
    text_changed();
  });

Demo Here

Solution 4:

give it a try :)

$("#search").change(function(){
      //your ajax codes
});

Solution 5:

<script>functionmulty(val)
{
alert(val.value+"--"+val.id);
}
</script><inputid="txtdemo"type="text"onchange="multy(this);"></br><inputid="txtdemo"type="text"onchange="multy(this);"></input>

Click to see out put screen

Thanks... :)

Post a Comment for "Asp Textbox Calls Javascript Function"