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>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742380608a4433039.html
评论列表(0条)