Llamada de reemplazo de carácter jQuery activada al hacer clic

I have this jquery function to automatically replace sertain characters with others:

jQuery(function() {
    jQuery('.textarea').keyup(function() {

    jQuery(this).val(jQuery(this).val().replace(/A/g, "5"));
    jQuery(this).val(jQuery(this).val().replace(/Q/g, "3"));

    });
});

I want to make this function optional and only activated if the user clicks on a button next the text-area input form.

Estoy intentando algo como esto:

$("a").toggle(
  function () {

        jQuery(function() {
            jQuery('.tifibox').keyup(function() {

            jQuery(this).val(jQuery(this).val().replace(/A/g, "3"));
            jQuery(this).val(jQuery(this).val().replace(/Q/g, "5"));

            });
        });

    $(this).addClass("selected"); // button selected
  },
  function () {
    $(this).removeClass("selected");
  }
);

I have not tested this yet.. but is that correct? thanks

preguntado el 27 de agosto de 11 a las 19:08

1 Respuestas

Change the link to a checkbox and then you can use something like:

$("#checkOne").change(   function () {
      if($(this).is(":checked")) {
         $("#textOne").bind("keyup", function() {
              $(this).val($(this).val().replace(/A/g, "3"));
         });
      } else {
          $("#textOne").unbind("keyup");
      }   }

);

El jsfiddle

This code will only replace newly typed text and old text will remain unchanged. If you want to replace the existing text (i.e. escape the existing text, modify the code as follows:

$("#checkOne").change(
  function () {
      var textarea = $("#textOne");
      if($(this).is(":checked")) {
         escapeText($(textarea));
         $(textarea).bind("keyup", function() {
              escapeText($(this));
         });
      } else {
          $(textarea).unbind("keyup");
      }
  }

);

function escapeText(ele) {
    $(ele).val($(ele).val().replace(/A/g, "3"));
}

los jsFiddle

Of course you can use a link as well, but the syntax is confusing. toggle() takes 3 parameters (3 functions):

1 - handler(eventObject)A function to execute every even time the element is clicked.

2 - handler(eventObject)A function to execute every odd time the element is clicked.

3 - handler(eventObject)Additional handlers to cycle through after clicks.

So in your code above, the first click would call the second function (since one it's odd) the second you call the first function. So turn on the feature in the second function and turn it off in the first.

También use bind y unbind as I have shown rather than setting classes.

Respondido 28 ago 11, 04:08

thanks for the code, I added a checkbox above the textarea with #myCheckbox ID, and in the "replace code here" comment I added the replace functions: jQuery(this).val(jQuery(this).val().replace(/A/g, "3")); but the character replacement didn't occur when I checked the box.. did I miss something here?.. - kumo99

Works fine. Here is the jsfiddle jsfiddle.net/ZDyn3/1 when I type an uppercase A it's replaced with 3 (if the checkbox is checked). If the checkbox is not checked A permanecerá A. I think you want to replace all current text if the checkbox is checked and continue to replace text if the checkbox is checked. Is that correct? The code as it is, doesn't change the current text only newly typed text. - Ali

Thank you so much, it worked now after I added: $(document).ready(function(){ - kumo99

I have multiple textareas in the same page, so the only problem is that it works only for the first textarea input field.. do you have any idea if it could work with multiple textareas? thanks again - kumo99

sorry I didn't make myself clear on the problem, what I mean is that each textarea has its own checkbox, so it doesn't work when I check the checkbox under other textareas to replace characters; it works for the first checkbox only. also the first checkbox if selected is affecting characters to be replaced in other textareas which is not what I want.. I have 3 textareas so I want them to be independent and only change characters if the checkbox under it is selected.. thanks again - kumo99

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.