最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change case to lowercase in JS text area - Stack Overflow

programmeradmin1浏览0评论

How do I change the case of a character in a textbox/textarea to lowercase onchange?

<html>
<head>
<title>Page Title</title>
<script>
function f2(string)
{
string=string.toUpperCase();
alert(string);
}
</script>
</head>
<body>
<p>Here are my text entry objects:</p>
<form>
<p>
Change the scripting (still using one function) so that the text from the alert is shown in uppercase from the textbox but lowercase from the textarea:<br>
<textarea onchange='f2(this.value);'></textarea>
</p>
</form>
</body>

How do I change the case of a character in a textbox/textarea to lowercase onchange?

<html>
<head>
<title>Page Title</title>
<script>
function f2(string)
{
string=string.toUpperCase();
alert(string);
}
</script>
</head>
<body>
<p>Here are my text entry objects:</p>
<form>
<p>
Change the scripting (still using one function) so that the text from the alert is shown in uppercase from the textbox but lowercase from the textarea:<br>
<textarea onchange='f2(this.value);'></textarea>
</p>
</form>
</body>
Share Improve this question edited Nov 14, 2011 at 0:03 methuselah asked Nov 13, 2011 at 23:56 methuselahmethuselah 13.2k52 gold badges176 silver badges332 bronze badges 1
  • Demo: jsfiddle/HDR8t – Jared Farrish Commented Nov 14, 2011 at 0:06
Add a ment  | 

8 Answers 8

Reset to default 3

Have you tried;

function f2(textarea)
{
    string = textarea.value;
    alert(string);
    string = string.toLowerCase();

    textarea.value = string;
}

With the modification to the onChange as;

<textarea onchange='f2(this);'></textarea>

Just use the .toLowerCase() method.

Simply change the value and assign it back.


<textarea onchange='this.value=this.value.toLowerCase();'></textarea>

Because nobody fixed your code

HTML:

<p>Here are my text entry objects:</p>
<form>
  <p>
    Change the scripting (still using one function) so that the text from the alert is shown in uppercase from the textbox but lowercase from the textarea:<br>
  <textarea></textarea>
</p>

JS:

document.getElementsByTagName("textarea")[0].addEventListener("change", function () {
    this.value = this.value.toLowerCase(); 
});

You want to add a change event handler. Inside the event handler you merely overwrite the value property of the element with the string changed to lowerCase.

I also fixed your in-line javascript in your HTML. It is the devil, avoid it.

Live Example

Use onchange='this.value = this.value.toUpperCase();' to make the text uppercase. Replace toUpperCase with toLowerCase for the opposite.

If desired, you can use your own function instead of just toUpperCase, passing either just the textarea's value or the entire textarea. For example (value only):

<!-- HTML -->
<textarea onchange='this.value = f2(this.value);'></textarea>

// JavaScript
function f2(oldText) {
    var newText = oldText.toUpperCase();
    return newText;
}

Or (entire textarea):

<!-- HTML -->
<textarea onchange='f3(this);'></textarea>

// JavaScript
function f3(ta) {
    ta.value = ta.value.toUpperCase();
}

I would pass this and then work on it like a DOMNode:

<p>Here are my text entry objects:</p>
<form>
 <p>
 Change the scripting (still using one function) so that the text from the alert is shown in uppercase from the textbox but lowercase from the textarea:<br>
  <textarea onchange='f2(this);'></textarea>
 </p>
</form>

function f2(el) {
    el.value = el.value.toLowerCase();
}

http://jsfiddle/HDR8t/1

Problem 1

I believe the onchange event only gets fired when the <textarea> no longer has focus. Instead, you'll want to use the onkeyup event.

Problem 2

You're only passing the string to the function. If you want to change the actual text in the <textarea>, you'll need to pass the actual DOM element to your function:

<textarea onkeyup="f3(this)"></textarea>

Problem 3

Once you pass the element into your function, you'll need to update its value attribute:

function f3(elem) {
    elem.value = elem.value.toLowerCase();
}

Try the [.toLowerCase()][1] method.

<textarea onchange='this.value=this.value.toLowerCase();'></textarea>
发布评论

评论列表(0)

  1. 暂无评论