0

hey all i am appending a form to a page on click the form has some text boxes and i need to add event listner like on keypress but the function dosent works dont know where is the problem the function works well everywhere but not for this form here is the code.

appending the form

 function activityCHART(thisobj){
 var theidis=$(thisobj).attr("id");
  $("#FULL_FADE").fadeIn();
      $.ajax({
  type: 'post',
    url: 'newpage.php',
  data:{'actde':theidis},
    success: function(dataa){
   $("#the_APPEDEDr5").empty().append(dataa);
  }});}

newpage this textbox is present and some more text areas

<input type="text" name="deptname" placeholder="department name" id="detp_names09o" class="TEXTNAME_o909ioi"/>

add this event listner

 $('#detp_names09o').keypress(function (e) {
alert('ok');});

these are some script links

 src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

i think there are some script link problem

alert comes when i does it like this onkeyup="thisisfun();" function thisisfun(){ alert('ok'); }

2
  • remove first jquery link, just go for <script src="ajax.googleapis.com/ajax/libs/jquery/1.10.2/…> and use $('#detp_names09o').on("keypress",function (e) { alert('ok'); }); Jan 29, 2015 at 6:05
  • nothing happens .. alert comes when i does it like this onkeyup="thisisfun();" function thisisfun(){ alert('ok'); }
    – Rahul aman
    Jan 29, 2015 at 6:12

4 Answers 4

1

You should use live(), delegate() or on() to attach event listeners to dynamically added DOM elements. bind() and keypress() doesn't work for elements that are dynamically added to DOM[see this]

$('#detp_names09o').live("keypress",function (e) {
      //do some stuff
});

.on() is mostly syntax sugar that can mimic .live(), or .delegate() depending on how you call it.

$('#detp_names09o').on("keypress",function (e) {
      //do some stuff
 });

Also, you have specified two different versions of jQuery. Though CDN's do have some advantages over locally referenced libraries, they might break your code at-times. If thats the reason you've referenced to local jQuery file(along with CDN version), you might consider looking at CDN fallbacks. In either case, you should be careful about the version you are using.

Cheers!

0

To attach event to dynamically added elements,

Try binding the event using 'bind'

$('#detp_names09o').bind("keypress",function (e) {
      alert('ok');
});

or use 'on'

$('#detp_names09o').on("keypress",function (e) {
          alert('ok');
 });

Also you dont require two versions of jquery in your page, also make sure this id is not duplicated

2
  • You dont require two versions of jquery in your page, also make sure this id is not duplicated, still if its is not working try creating a fiddle
    – ssilas777
    Jan 29, 2015 at 5:54
  • alert comes when i does it like this onkeyup="thisisfun();" function thisisfun(){ alert('ok'); }
    – Rahul aman
    Jan 29, 2015 at 6:14
0

use onkeyup,.. attribute inside the element and call the function like this

<input type="text" name="deptname" placeholder="department name" id="detp_names09o" class="TEXTNAME_o909ioi" onkeyup="functionName()"/>

in javascript

function functionName(){
  //your code
}
3
  • i know i can do it like this but here i need to add event listners there are many ..
    – Rahul aman
    Jan 29, 2015 at 6:13
  • in that case jquery 1.8 or 1.7 supports $('#elementid').live("click",function(){}); Jan 29, 2015 at 6:16
  • avoid adding inline script on html. Jan 29, 2015 at 6:17
0

First of all you should decide what do u want to use, keyup or keypress ? For example if you want to use keyup and you are using jquery version greater than 1.7 then use

$(document).ready(function () {
    $('#element').on("keyup",function () {
              alert('result ok');
    });
});

else u can use

 $(document).ready(function () {
    $('#element').live('keyup', function() {
      alert('result ok');
    });
 });

Make sure that you are calling working script (check your script link), try not to make duplicate ids of elements instead use class and avoid using inline use of javascript. Happy Coding !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.