Separate non-alpha tractors with jQuery when copying text from one input to another

On my website, I have two input fields: Title and Stock Code. I have two goals:

  • The text from the "Title" field should be copied to the stock when the user enters the text in the header field.

  • I also want non-alphanumeric carnations to be removed during the copy.

I found this tutorial that explains how to make the first goal. Here is my working example:

http://jsfiddle.net/2gezC/1/

In the second part, it’s hard for me to find a tutorial on removing characters in jQuery, since most of the tutorials are for JavaScript.

I found this tutorial and tried to integrate it into my code as follows:

(function ($) {
    $(document).ready(function () {

        $('input#edit-title-fragment').keyup(function () {
            var str = $(this).val();
            str = str.replace(/[^a-zA-Z 0-9]+/g, '');
            var txtClone = $(this).val();
            $('input#edit-sku-fragment').val(txtClone);
        });
    });

})(jQuery);

. - , . !

+5
1

:

// Get the user input from "this" and put it in str variable
var str = $(this).val();
// Remove all non alpha-nums from str and store it back in the str variable
str = str.replace(/[^a-zA-Z 0-9]+/g, '');
// Get the user input from "this" (yes, again) and put it in the txtClone variable
var txtClone = $(this).val();
// Set your other textbox to be the value in txtClone
$('input#edit-sku-fragment').val(txtClone);

? str.

$('input#edit-sku-fragment').val(str);

: http://jsfiddle.net/2gezC/2/

Furthmore, , jQuery - , JavaScript; JavaScript, , ; https://github.com/jquery/jquery

+15

All Articles