admin管理员组文章数量:1336340
Button – calls a function to determine if the string in the textbox is found in the contents of the textarea. Naturally, message to the user if the string was found or not.
Those were the instructions given to follow, Im not sure exactly how to do this. Here is what I came up with, I keep getting this console error:
TypeError: document.getElementById(...) is null
file:///........program02javascript/program02script.js
Line 10line 10 : var SearchTerm = document.getElementById("text_box_1").value;
Here is my HTML:
<div id="requirement #2">
<h1>Requirement #2</h1>
<form>
Search For:
<input type="text" name="text_box_1">
<br>
</form>
<textarea id="text_area_3"></textarea>
<button type="button" id="button2" onclick="StringSearch()">Search</button>
</div>
Here is my Javascript:
function StringSearch() {
var SearchTerm = document.getElementById("text_box_1").value;
var TextSearch = document.getElementById("text_area_3").value;
if (TextSearch.match(SearchTerm) === "") {
alert("String Found. Search Complete");
} else {
alert("No Data found in Text Area");
}
}
How can I acplish this task? I'm not sure I understand pletely what to do. I was trying to find some example code but haven't e across anything that's been helpful.
Button – calls a function to determine if the string in the textbox is found in the contents of the textarea. Naturally, message to the user if the string was found or not.
Those were the instructions given to follow, Im not sure exactly how to do this. Here is what I came up with, I keep getting this console error:
TypeError: document.getElementById(...) is null
file:///........program02javascript/program02script.js
Line 10line 10 : var SearchTerm = document.getElementById("text_box_1").value;
Here is my HTML:
<div id="requirement #2">
<h1>Requirement #2</h1>
<form>
Search For:
<input type="text" name="text_box_1">
<br>
</form>
<textarea id="text_area_3"></textarea>
<button type="button" id="button2" onclick="StringSearch()">Search</button>
</div>
Here is my Javascript:
function StringSearch() {
var SearchTerm = document.getElementById("text_box_1").value;
var TextSearch = document.getElementById("text_area_3").value;
if (TextSearch.match(SearchTerm) === "") {
alert("String Found. Search Complete");
} else {
alert("No Data found in Text Area");
}
}
How can I acplish this task? I'm not sure I understand pletely what to do. I was trying to find some example code but haven't e across anything that's been helpful.
Share Improve this question edited Oct 27, 2019 at 16:27 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 4, 2015 at 3:24 kronis72kronis72 3111 gold badge5 silver badges12 bronze badges2 Answers
Reset to default 3You haven't added
id
to the element,getElementById
selects the element from DOM based on the value ofid
attribute not thename
attribute.Add the
id
attribute to the textboxSearch For:<input type="text" name="text_box_1" id="text_box_1"><br> // ^^^^^^^^^^^^^^^
You're using
match()
to check if the match is found, but you can useindexOf
to check if the string is present in another.Add validation if the user has entered anything to search, if not return.
Demo
function StringSearch() {
var SearchTerm = document.getElementById("text_box_1").value;
var TextSearch = document.getElementById("text_area_3").value;
if (SearchTerm.length > 0 && TextSearch.indexOf(SearchTerm) > -1) {
alert("String Found. Search Complete");
} else {
alert("No Data found in Text Area");
}
}
<div id="requirement #2">
<h1>Requirement #2</h1>
<form>
Search For:
<input type="text" name="text_box_1" id="text_box_1">
<br>
</form>
<textarea id="text_area_3"></textarea>
<button type="button" id="button2" onclick="StringSearch()">Search</button>
</div>
function StringSearch() {
var SearchTerm = document.getElementById("text_box_1").value;
var TextSearch = document.getElementById("text_area_3").value;
if (SearchTerm.length > 0 && TextSearch.indexOf(SearchTerm) > -1) {
alert("String Found. Search Complete");
} else {
alert("No Data found in Text Area");
}
}
<div id="requirement #2">
<h1>Requirement #2</h1>
<form>
Search For:
<input type="text" name="text_box_1" id="text_box_1">
<br>
</form>
<textarea id="text_area_3"></textarea>
<button type="button" id="button2" onclick="StringSearch()">Search</button>
</div>
本文标签: javascriptSearch for a string from a textbox in a textareaStack Overflow
版权声明:本文标题:javascript - Search for a string from a textbox in a textarea - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742380608a2464013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论