-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhighlight-words-javascript-example.html
67 lines (53 loc) · 1.63 KB
/
highlight-words-javascript-example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<h1>Highlight clicked word</h1>
<style>
<!--
span.white { background-color: #FFFFFF; }
span.highlight { background-color: yellow; }
A:link {text-decoration: none; color: black;}
A:visited {text-decoration: none; color: black;}
A:active {text-decoration: none; color: black;}
A:hover {text-decoration: none; color: black;}
//-->
</style>
<script type="text/Javascript">
var sentence = "This is a test .";
var words = sentence.split(/\s/);
var initialhighlights = "";
var highlights = initalizeBooleanArray(words.length, initialhighlights);
writeSentence(words);
function initalizeBooleanArray(length, indexOfTruesString) {
var array = new Array(length);
for (i = 0; i < array.length; i++) {
array[i] = false;
}
return array;
}
// This method wraps every word in a sentence with a javascript click function.
function writeSentence(words) {
for(i = 0; i < words.length; i++) {
var word = words[i];
document.write('<span class="white" id="word.' + i + '">');
document.write('<a href="javascript:clickWord(' + i + ')"> ');
document.write(word);
document.write('</a>');
document.write('</span>');
}
}
function clickWord(x) {
highlights[x] = (!highlights[x]);
var y = 0;
var word = document.getElementById("word."+x);
if(highlights[x]) {
word.className = "highlight";
} else {
word.className = "white";
}
}
</script>
<form name="mturk_form" method="get" action="edit">
<input type=hidden name=highlights value="unchanged" />
<br><font size=-1 color=gray>Comments</font><br>
<textarea name=comment rows=5 cols=30> </textarea>
<br>
<input type=submit name="submit" value="Submit">
</form>