Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3178

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T12:18:07+00:00 2024-11-26T12:18:07+00:00

Introduction to JavaScript Series – #1 – Form with String Object

  • 61k

Let's design a text editor where you can input your text and get a result of the number of words in your text using HTML, JavaScript and CSS styling.

<!--Code Segment1--> <!DOCTYPE html> <html> <head>   <title>Form with String object</title> </head> 
Enter fullscreen mode Exit fullscreen mode

In this portion of code we are giving a title to our webpage which is “Form with String object” inside the <title> tag, which is embedded inside the <head> tag.

<!--Code Segment1--> <body style="background-color:orange"> 
Enter fullscreen mode Exit fullscreen mode

Inside the style attribute of the <body> tag, we are specifying the CSS styling of background-color to orange. This will set the background color of the webpage to orange.

<!--Code Segment3--> <h3>Enter your text in the given text area</h3> <form name="wordcount">   <textarea name="wordcount2" rows="12" cols="38" wrap="virtual" style="color:white;background-color:black;font-size:20px;"></textarea><br></br>   <input type="button" value="Calculate Words" onClick="Countit()">   <input type="text" name="wordcount3" size="20"> </form> 
Enter fullscreen mode Exit fullscreen mode

In this code segment, we are giving a name to the <form> which is wordcount and specifying the text area, in which the user will input the text. The text area name is given as wordcount2, which will be helpful for JavaScript to access the specific document elements. We are prescribing this specific styling to the text area:

  1. Number of rows in height: 12
  2. Number of Columns in width: 38
  3. Wrap attribute: virtual (The wrap attribute specifies how the text in a text area is to be wrapped when submitted in a form. Wrap when set to virtual means long lines are wrapped in the textarea, but are not wrapped in the data sent to the processing script.)
  4. Text color is white and font size of text is set as 20px.
  5. Background color is set as black for the text area.

A button is also placed with value Calculate Words, which on pressing will show the results of the number of words i.e. invoke the Countit() function. And a text area is specified which will display the output.

<!--Code Segment4--> <script type="text/javascript">     function Countit()     {       var formcontent = document.wordcount.wordcount2.value       formcontent = formcontent.split(" ")       document.wordcount.wordcount3.value = formcontent.length     } </script> 
Enter fullscreen mode Exit fullscreen mode

We have created a function named Countit() inside the <script> tag. Inside this function, we have initiated a String object formcontent which will hold the text being entered by the user, referenced by the document element names. Then, we call the split() function on the String object and store the result in the same object.

Use of split() function:
Split a string into an array of substrings
Eg:
var str = “How are you doing today?”;
var res = str.split(” “);
Output:
[“How”, “are”, “you”, “doing”, “today?”]

Then we count the length of the array String object formcontent using the length attribute of the array and output the result in the value attribute of the text element wordcount3.

<!--Code Segment5--> </body> </html> 
Enter fullscreen mode Exit fullscreen mode

This is the ending of the code segment.

Output:

img

Thanks for Reading,

Keep an eye on the next few articles on the same series.

Follow: @subhajit_saha

introductionjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.