/    Sign up×
Community /Pin to ProfileBookmark

Typing slider (cursor). Explanation of DOM manipulations.

I need to cut the program to the final logic cut. It’s now ~10 times smaller, although it’s as far as I could get in my understanding now.

Given two identical text strings. When you write in <input> you should write letters which have to be identical to the given line. And while you’re typing the cursor moves until the excercise_results().

My program is still big, and I need some help in understanding it’s actions. I know that each text contains a final dot and a space after it. But I couldn’t link this with the number “2” in for loop (after hiding the color of the <p >passage -…textDecoration “none”):
[B]length – 2
[i + 2][/B]

the second:
[B]extraNodes > 2
[pos + 2][/B]

and the third:
[B](k – 1)[/B]
and
[B](k – 2)[/B]

[CODE]<body>
<p id=”isdivNode”><span>Staff. </span>
</p>
<form>
<input size=”55″ onkeypress=”compare(event)”>
</form>
<script>
var strtest = “Staff. “;
divNode = document.getElementById(“isdivNode”);
var xp = 0;
deep = true;
divNode.childNodes[0].style.textDecoration = “none”;
for (i = 0; i < strtest.length; i++) {
if (i < divNode.childNodes.length – 2) {
if (divNode.childNodes[i + 2].nodeType == 1) {
divNode.childNodes[i + 2].firstChild.data = strtest.charAt(i);
}
} else {
myNode = divNode.firstChild.cloneNode(deep);
myNode.firstChild.data = strtest.charAt(i);
divNode.appendChild(myNode);
}
}
extraNodes = divNode.childNodes.length – strtest.length;
pos = strtest.length;
while (extraNodes > 2) {
divNode.childNodes[pos + 2].firstChild.data = “”;
pos++;
extraNodes–;
}
divNode.childNodes[0].style.color = “white”;
document.forms[0].elements[0].value = “”;
function compare(e) {
var k = strtest.length;
if (xp < (k – 1)) {
var keyChar = String.fromCharCode(e.which);
if (strtest.charAt(xp) == keyChar) {
divNode.childNodes[xp + 2].style.textDecoration = “none”;
xp += 1;
if (xp > (k – 2)) excercise_results();
divNode.childNodes[xp + 2].style.textDecoration = “underline”;
}
}
}
function excercise_results() {
confirm(“fin”);
}
</script>[/CODE]

I’m not at all experienced with DOM. It’s hard for me to link conditional logic with the DOM manipulations. And I can’t cut the program anymore (to have one/two “straight” conditions to see how it’s work and logic) – to get the cursor just pointing to another character to match.

Thanks in advance.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@deathshadowOct 16.2014 — I'm trying to figure out what the devil you are doing all that DOM manipulation for on what appears to be a simple string compare... you've got a whole slew of checks and compares and worse, style manipulation from the scripting for... well... Christmas only knows what.
×

Success!

Help @xcislav spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...