I have a problem in jQuery: I wrote this code:
<!DOCTYPE html>
<html>
<head>
<script src=".11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#input").keyup(function() {
var input = $("#input").val();
var input = input.replace("d", "f");
$("#text").val(input);
});
});
</script>
</head>
<body>
<center>
<input type="text" id="input" />
<div id="text"></div>
</center>
</body>
</html>
This code's suppose to check if there are any d's in the the variable "input" (which is received from an input box in the body) and replace those d's with f's (this is just a little experiment before I try to replace letters of one language to letters of another).
but it doesn't work and I don't know why.
I tried to replace the $("#text").val(input);
with alert(input);
and it worked but it only replaces the first "d" to "f" and the second and third d's aren't replaced and they stay "d".
Please help me.
Thank you.
I have a problem in jQuery: I wrote this code:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#input").keyup(function() {
var input = $("#input").val();
var input = input.replace("d", "f");
$("#text").val(input);
});
});
</script>
</head>
<body>
<center>
<input type="text" id="input" />
<div id="text"></div>
</center>
</body>
</html>
This code's suppose to check if there are any d's in the the variable "input" (which is received from an input box in the body) and replace those d's with f's (this is just a little experiment before I try to replace letters of one language to letters of another).
but it doesn't work and I don't know why.
I tried to replace the $("#text").val(input);
with alert(input);
and it worked but it only replaces the first "d" to "f" and the second and third d's aren't replaced and they stay "d".
Please help me.
Thank you.
- Please check my answer with demo it's working as per your need, only small change in your code – Yogesh Sharma Commented Jun 20, 2014 at 11:16
9 Answers
Reset to default 4You need html()
or text()
for div instead of val()
$("#text").html(input);
suggestion:please use right name to identify
replcae: val() to html()
<script>
$(document).ready(function() {
$("#input").keyup(function() {
var input = $("#input1").val();
var input = input.replace("d", "f");
$("#text").html(input);
});
});
</script>
</head>
<body>
<center>
<input type="text" id="input1" />
<div id="text1"></div>
the default .replace() behavior is to replace only the first match, the /g modifier (global) tells it to replace all occurrences. So you need to write your function like below.
$(document).ready(function() {
$("#input").keyup(function() {
var input = $(this).val().replace(/d/g, "f");
$("#text").html(input);
});
});
Fiddle Demo
for div
you need to use .text()
instead of .val()
because there is no value
attribute present for div
:
$(document).ready(function() {
$("#input").keyup(function() {
var input = $("#input").val();
var input = input.replace("d", "f");
$("#text").text(input);
});
});
For text
$("#divname").text("new text");
for html
$("#divname").html("<b>new html</b>")
Try replaceAll() method instead of replace.
You have non input tag use .text()
or .html()
. if you have input type then only you have to use .val()
;
$("#text").text(input);
replace
will only replace the string for the first time. To perform a global replace use the below code. To assign the value to the div use either $("#text").html(input);
or $("#text").text(input);
$("#input").keyup(function () {
var input = $("#input1").val();
var input = input.replace(/d/g, "f");
$("#text").html(input);
});
Please use below jQuery code:
$(document).ready(function() {
$("#input").keyup(function() {
var input = $("#input").val();
var input1 = input.replace("d", "f");
$("#text").html(input1);
});
});
It's working see demo: http://jsfiddle/V2LBu/1/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744957701a4603294.html
评论列表(0条)