0

I've been trying to get this code to work:

<!DOCTYPE html>
<html>
<body>
<input name="paragraph" id="paragraph" value="Paste your paragraph here!" style="width: 800px; height: 100px;"><br>
<input name="search" id="search" value="(Case sensitive) Text to search for" style="width: 400px;"><br>
<button type="button" onclick="myFunction()">Find text</button><br>
<script>
function myFunction() {
    text = document.getElementById("paragraph");
    var textSearch = document.getElementById("search");
var hits = [];


for(var i = 0; i < text.length; i++) {
    if(text[i] == "J"){
    for (var j = i; j < (textSearch.length + i); j++) {
        hits.push(text[j]);
    }

    if (hits === 0) {
        alert('Your name isn't here')
    } else {
        alert(hits);
    }
}
</script>
</body>
</html>

Basically, what it's trying to do is for the user to enter a paragraph or big chunk of text into the top box, then some text they're trying to search for in the lower box. Then hit a button and it displays where in the code that it is (by telling the user how many characters it went through, until it hit what they were searching for). If you could improve it, or show me something from scratch that would be better!

6
  • 1
    if(text[i] == "J") ? this line for. Commented Nov 27, 2013 at 4:16
  • you wanna search one string in other ? Commented Nov 27, 2013 at 4:18
  • If only want to first occurrence can use text.indexOf(textSearch) Commented Nov 27, 2013 at 4:21
  • 1
    it has many errors of quotes (single/double quotes) writing Commented Nov 27, 2013 at 4:26
  • 1
    also, you're not declaring your text parameter. This should be var text = ...; Commented Nov 27, 2013 at 4:26

1 Answer 1

2

Here's the fixed version of yours :D

<!DOCTYPE html>
<html>
<head>
<title>
Cari teks
</title>
<script>
function myFunction() {
    var text = document.getElementById("paragraph").value;
    var textSearch = document.getElementById("search").value;

var hits = [];
var kata = text.split(" ");
var jmlkata =  kata.length;
var i = 0;
for (i=0;i<=jmlkata-1;i++){
    if (textSearch == kata[i]){
    hits.push(kata[i]);
    }
}
var jmlfound = hits.length;
      alert("found : "+jmlfound);

}
</script>
</head>
<body>
<textarea name="paragraph" id="paragraph" style="width: 800px; height: 100px;" > 
</textarea><br>
<input name="search" id="search" style="width: 400px;"><br>
<button type="button" onclick="myFunction()">Find text</button><br>
</body>
</html>

I'm sorry about the words/language, some parts of this using Bahasa Indonesia, but it won't be a matter.
I replace your large text input with a textarea, So it would be more powerfull to have long text.
The search engine of this works looking for 1 (one) word only. So you have to develop this script.
just use it as a reference, may it helps :D

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.