skip to Main Content

I have a webpage with a large scrollable textarea element, which contains several pages of text. How can I identify which lines of that text are currently on display using JavaScript?

I can use scrollTop to find out approximately how far the user has scrolled through the textarea, but that doesn’t allow me to identify exactly the text on display because the text at the top frequently has line wrapping, whereas the text at the bottom does not (so if scrollTop is 50% of scrollHeight, the text is less than 50% of the way through the lines of text).

Background: The textarea, on the left of the screen, contains text in a markup language. A display area, on the right of the screen, shows the data as html-formatted text. When I scroll through the marked up text in the textarea on the left, I want the html-formatted display area to scroll in sync. However because of formatting differences I cannot just use the scrollTop and scrollHeight.

Any suggestions? I’m looking for a solution in HTML, JavaScript and/or CSS. Thanks.

2

Answers


  1. const quizData = [{
            question: "Which of the following is a client site language?",
            a: "Java",
            b: "C",
            c: "Python",
            d: "JavaScript",
            correct: "d",
        },
        {
            question: "What does HTML stand for?",
            a: "Hypertext Markup Language",
            b: "Cascading Style Sheet",
            c: "Jason Object Notation",
            d: "Helicopters Terminals Motorboats Lamborginis",
            correct: "a",
        },
        {
            question: "What year was JavaScript launched?",
            a: "1996",
            b: "1995",
            c: "1994",
            d: "none of the above",
            correct: "b",
        },
        {
            question: "What does CSS stands for?",
            a: "Hypertext Markup Language",
            b: "Cascading Style Sheet",
            c: "Jason Object Notation",
            d: "Helicopters Terminals Motorboats Lamborginis",
            correct: "b",
        }
    ];
    let index = 0;
    let correct = 0,
        incorrect = 0,
        total = quizData.length;
    let questionBox = document.getElementById("questionBox");
    let allInputs = document.querySelectorAll("input[type='radio']")
    const loadQuestion = () => {
        if (total === index) {
            return quizEnd()
        }
        reset()
        const data = quizData[index]
        questionBox.innerHTML = `${index + 1}) ${data.question}`
        allInputs[0].nextElementSibling.innerText = data.a
        allInputs[1].nextElementSibling.innerText = data.b
        allInputs[2].nextElementSibling.innerText = data.c
        allInputs[3].nextElementSibling.innerText = data.d
    }
    
    document.querySelector("#submit").addEventListener(
        "click",
        function() {
            const data = quizData[index]
            const ans = getAnswer()
            if (ans === data.correct) {
                correct++;
            } else {
                incorrect++;
            }
            index++;
            loadQuestion()
        }
    )
    
    const getAnswer = () => {
        let ans;
        allInputs.forEach(
            (inputEl) => {
                if (inputEl.checked) {
                    ans = inputEl.value;
                }
            }
        )
        return ans;
    }
    
    const reset = () => {
        allInputs.forEach(
            (inputEl) => {
                inputEl.checked = false;
            }
        )
    }
    
    const quizEnd = () => {
        // console.log(document.getElementsByClassName("container"));
        document.getElementsByClassName("container")[0].innerHTML = `
            <div class="col">
                <h3 class="w-100"> Hii, you've scored ${correct} / ${total} </h3>
            </div>
        `
    }
    loadQuestion(index);
    
    Login or Signup to reply.
  2. <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <link rel="stylesheet" href="css/custom.css" />
    </head>
    
    <body>
        <section class="main">
    
            <div class="container">
                <div class="col">
                    <h3 id="questionBox">
                        1) Lorem ipsum dolor sit amet, consectetur adipisicing elit Debitis?
                    </h3>
                </div>
                <div class="col box">
                    <input name="option" type="radio" id="first" value="a" required>
                    <label for="first">Testing 1</label>
                </div>
                <div class="col box">
                    <input name="option" type="radio" id="second" value="b" required>
                    <label for="second">Testing 2</label>
                </div>
                <div class="col box">
                    <input name="option" type="radio" id="third" value="c" required>
                    <label for="third">Testing 3</label>
                </div>
                <div class="col box">
                    <input name="option" type="radio" id="fourth" value="d" required>
                    <label for="fourth">Testing 4</label>
                </div>
                <button id="submit">Submit</button>
            </div>
    
        </section>
        <script src="js/app.js"></script>
    </body>
    
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search