SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Selecting highlighted text

    Hi can anyone help I want to build a very simple activity in Javascript.

    It needs to allow the user to highlight some text within a paragraph. If the correct text (with an error) is selected it should provide feedback that the user has correctly selected this text.

    So what I need to do is

    1. Get the text selection
    2. Compare this text selection to a predefined answer
    3. Provide feedback to the user e.g. they are correct if text selection = predefined answer, and incorrect if text selection != predefined answer

    Can anyone help?

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to use the TextRange object to achieve this in IE. Range object for Mozilla.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I provide just one answer?

    I found a way to get this function to work, but I only want it to write a correct or incorrect answer once. Currently it keeps printing another answer each time the link is clicked - how do I get it to run just once?

    Steven

    <script type="text/javascript">
    <!--
    function getSel()
    {
    var txt = ''";

    if (window.getSelection)
    {
    txt = window.getSelection();
    }
    else if (document.getSelection)
    {
    txt = document.getSelection();
    }
    else if (document.selection)
    {
    txt = document.selection.createRange().text;
    }
    else return;
    if (txt == "answer" ){
    txtfeedback = 'You have answered correctly. This is an example of "copying word for word directly from the text"';
    } else {
    txtfeedback = 'You have answered incorrectly. Please have another go.';
    }

    var feedbackdiv = document.getElementById("feedbackdiv");

    var para = document.createElement("p");

    var txtfeedback = document.createTextNode(txtfeedback);

    para.appendChild(txtfeedback);

    feedbackdiv.appendChild(para);




    }
    // -->
    </script>

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do you call getSel function?


Bookmarks

Posting Permissions

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