/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Javascript problem

Q1- Hi all, I have a project that there is a page with textbox and textarea and 2 buttons, I should write a string (word or more) in the textbox then when I click on the “Search” button it should open a window.prompt, which I should enter a char or word and then it should return how many times this word or character is found in the string entered in the textbox and then show it in the textarea, Here is what I have but it’s not working:

[CODE]
<html>
<head>
<title>Page 1</title>
<script type=”text/javascript”>

function search()
{
var a = window.prompt(“Enter the string you want to search for:”,0);
var b = F1.T1.value;
var count = 0;
for (var i = 1; i <= b.length; i++)
{
if (b.indexOf(a,i))
{
count = count + 1;
}
}
F1.A1.value = count;
}
</script>
</head>

<body>
<br /><br />
<form name=”F1″>
<p>Enter value:</p>
<input type=”text” name=”T1″ size=”20″ />
<input type=”button” name=”S1″ value=”Search” onclick=”search()” />
<input type=”button” name=”S2″ value=”Show” onclick=”show()” />
<br />
<p>This is the result:</p>
<textarea cols=”30″ rows=”6″ name=”A1″ /></textarea>
</form>
</body>
</html>
[/CODE]

Q2- There is another button named as “Show” .. it should take the string in the textbox and return it like this: (using a function called “show()”..

Ex: if the string is “world” then it should write the following in the textarea:

w
wo
wor
worl
world

So can someone help me please with these two questions,
Thanks in advance,
zeid

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Dec 04.2006 — strings begin at index 0 not 1 and they end at length-1 because of the shift.

Second question, start coding....

Eric
Copy linkTweet thisAlerts:
@zeidauthorDec 05.2006 — ok but there is a problem, that this code is giving me the length of "a" ( or the string entered ) not how many times is "b" found in a..
Copy linkTweet thisAlerts:
@zeidauthorDec 05.2006 — I have fixed it.. thanks
×

Success!

Help @zeid 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.20,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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