jQuery, if var = 1 do THIS -


the code below populates disabled form field contents of field user types in it.

the disabled form field enabled when 'edit' clicked, , shows 2 more options 'cancel' , 'save'.

upon clicking 'edit' variable 'counter' set = 1

here falling short...

if counter == 1 auto population of newly enabled field should halted.

however, despite counter variable being updated auto-population still active.

any ideas?

jsfiddle here.

$(document).ready(function(){ var counter = 0 $('#filename_edit').click(function(){     $('#filename').prop('disabled', false);     $(this).hide();     $('#filename_edit_btns').show();     counter = 1;     alert(counter); }); $('#filename_edit_cancel').click(function(){     $('#filename').prop('disabled', true);     $(this).parent().hide();     $('#filename_edit').show();     counter = 0;     alert(counter); }); $('#filename_edit_save').click(function(){     $('#filename').prop('disabled', true);     $(this).parent().hide();     $('#filename_edit').show();     counter = 1;     alert(counter); }); if (counter == 0) {     $('#title').bind('keyup keypress blur', function() {           var mystr = $(this).val()             mystr=mystr.tolowercase();             mystr=mystr.replace(/[^a-za-z0-9 ]+/g,"");             mystr=mystr.replace(/\s+/g, "-");          $('#filename').val(mystr);     }); } }); 

if right, don't want filename field auto-populated when edit on.

but bind function @ page load, if counter changes, bind still on, , function trigger.

one solution move test bind :

$('#title').bind('keyup keypress blur', function() {               if (counter == 0) {                              //<--- here         var mystr = $(this).val();         mystr=mystr.tolowercase();         mystr=mystr.replace(/[^a-za-z0-9 ]+/g,"");         mystr=mystr.replace(/\s+/g, "-");         $('#filename').val(mystr);     } }); 

here's working demo


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -