admin管理员组文章数量:1393908
I'm trying to make a textarea input field which automatically replaces specific characters to different characters; example: when a user types "a" character it should be automatically replaced with "o" character. I'm new to jquery so can you please tell what's wrong with the following code:
$(function() {
$('#myTextBox').keyup(function() {
$("a").replaceWith( "o" );
$("z").replaceWith( "N" );
$("y").replaceWith( "M" );
$("p").replaceWith( "f" );
$("v").replaceWith( "K" );
$("b").replaceWith( "P" );
});
});
I appreciate your help, thank you
edit:
thank you all, the following worked as expected:
$(function() {
$('#myTextBox').keyup(function() {
$(this).val($(this).val().replace(/a/g, "o"));
});
});
I'm trying to make a textarea input field which automatically replaces specific characters to different characters; example: when a user types "a" character it should be automatically replaced with "o" character. I'm new to jquery so can you please tell what's wrong with the following code:
$(function() {
$('#myTextBox').keyup(function() {
$("a").replaceWith( "o" );
$("z").replaceWith( "N" );
$("y").replaceWith( "M" );
$("p").replaceWith( "f" );
$("v").replaceWith( "K" );
$("b").replaceWith( "P" );
});
});
I appreciate your help, thank you
edit:
thank you all, the following worked as expected:
$(function() {
$('#myTextBox').keyup(function() {
$(this).val($(this).val().replace(/a/g, "o"));
});
});
Share
Improve this question
edited Nov 6, 2015 at 14:51
Ali Erfani
6921 gold badge11 silver badges27 bronze badges
asked Jun 11, 2011 at 15:24
Sami joki Sami joki
231 silver badge4 bronze badges
3
-
The jquery selector is meant for selecting dom elements. Not to select value from a text box. you should be using the
val()
function to get the values – Sujit Agarwal Commented Jun 11, 2011 at 15:28 - RE: "is that correct?" Well, have you tried it in your application? – Kon Commented Jun 11, 2011 at 15:58
- I changed the code an used val() function, but not sure if that's correct.. – Sami joki Commented Jun 11, 2011 at 15:58
2 Answers
Reset to default 4First of all $("a")
actually targets all anchor elements on the page, so I'm guessing that's not what you want (and others don't even exist, except p, so your selector will return nothing). Secondly, you can use regular expressions to do your replace without any special jQuery code.
Instead of:
$("a").replaceWith( "o" );
Try:
$(this).val($(this).val().replace(/a/g, "o"));
To break that down:
var oldValue = $(this).val();
var newValue = oldValue.replace(/a/g, "o");
// Set to new value
$(this).val(newValue);
http://jsfiddle/2fYdT/69/
$(document).ready(function() {
$(".normal").each(function() {
var text = $(this).html();
text = text.replace(/"/g, '');
$(this).html(text);
});
});
for something like this:
<div class="normal">Lorem "ipsum "dolor"</div>
Result:
<div class="normal">Lorem ipsum dolor</div>
本文标签: javascripthow to Replace each character with other character in JqueryStack Overflow
版权声明:本文标题:javascript - how to Replace each character with other character in Jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744068467a2585448.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论