www.webdeveloper.com
Results 1 to 4 of 4

Thread: need help in defining function

  1. #1
    Join Date
    Nov 2008
    Posts
    10

    need help in defining function

    Hello,

    I am writing code for my form which has 2 textbox One is labelled as "search" and the other one is "replace" and one textarea. If a user enters "hello how are you" in the textarea and user needs to change that hello to hi, he/she needs to enter "hello" in search textbox and "hi" in "replace" textbox. There is another button that says "replace now", if user clicks that button hello changes to hi.

    Can anyone give me some hints please. I am so confused how can I define such function?

    Thank you,

  2. #2
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    every field value read by javascript is considered as a string. so, the obvious function to use is the built-in replace method inherited by strings:
    Code:
    string.replace(stringToFind, stringToReplaceWith);
    string.replace();. basically you would create a custom function that will combine what the user typed in the search box and in the replace box and pass them as arguments to replace method. finally, the result is set back to the textbox that contain the text.
    my mom is javascript, dad is javascripter, granpa is javascriptor, and my little sister is javasRidiculous.
    my nature language is javascript, then come spanish and english -- me

  3. #3
    Join Date
    Nov 2008
    Posts
    10
    Thanks zerokilled.
    Now I got the concept.
    However, I dont want my text in textarea to be hardcoded. User can enter any sentence and they should be able to change their words in textarea using search and replace.
    Do I do that like this?

    var str = replaceform.txt.value; //replaceform is the name of the form; txt is the textarea's id
    document.write(str.replace(/how can I get search's text area value?/, "how can I get ")replace's text area value?);

    what goes inside str.replace()??????? How can I get the user entered text area's value from search and replace textarea??

    Thanks,

  4. #4
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    basically you will have at least 3 fields: textarea that contain the user's text (txt), input text for string to search (sech), and input text for text to replace with (repl). and the function would be something like follow:
    txt.value = txt.value.replace(sech.value, repl.value);
    assuming that txt, sech and repl are reference to each respective element. the value property refer to the containing text of each field.
    my mom is javascript, dad is javascripter, granpa is javascriptor, and my little sister is javasRidiculous.
    my nature language is javascript, then come spanish and english -- me

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles