Navigate to a specific position with a single click by accessing a class from another Vue component

Clarification of the issue

When a user clicks on the login link, the view should automatically scroll down to the login window where they can input their credentials.

I know how to achieve this in a single file using

document.getElementById('login-window').scrollIntoView()

However, I am working on a project that consists of multiple Vue.js component files. The login link is part of a "label" component, while the login window itself is located in another component called "loginWindow", which also holds the id/class "login-window".

I attempted to access the "login-window" element using getElementById within my "label" component, but it seems unable to retrieve it due to being in a different component.

The template code from "loginWindow"

<template>
  <LoginGrid class="login-window" :as-grid="true" :class="classes" autocomplete="off">
    <OCard class="login-card" :border="false">
      <div class="login-headline f-copy f-bold l-color-primary">{{ t('headline') }}</div>
      <!-- online state -->
      <template v-if="isLogged">
        <OButton color="secondary" @click="onClickLogout">{{ t('logout-label') }}</OButton>
      </template>
      <!-- offline state -->
      <template v-else>
        <div class="login-inputs">
          <LoginInput
            class="login-input-card-number" />
            ...
        </div>

        ...
        <OLink
          type="secondary"
          class="login-mode-link f-regular f-copy-small"
          :no-router="true"
          @click="onSwitchMode"
        >
          {{ modeLabel }}
        </OLink>
        ...
      </template>
    </OCard>
  </LoginGrid>
</template>

My attempted solution

In my "label" component, I have implemented the following method with an else-statement:

export default {
  name: 'loginWindow',

  ...

  methods: {
    onClick() {
      if (this.isLogged) {
       ...
      } else {
          if (isBrowser) {
            document.getElementById("login-window").scrollIntoView();
          }
      }
    },
  },
}

So when the user is not logged in, clicking on the login link triggers onClick() to scroll to the id "login-window".

Unfortunately, this approach does not work as expected and results in the error message "Cannot read property 'scrollIntoView' of null".

Any suggestions on how to achieve this using JavaScript within a Vue.js component?

Answer №1

login-screen is not an ID but a Class in your HTML code. Update it with the following:

document.querySelector(".login-screen").scrollIntoView();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating separate versions (development and production) of JavaScript code: a step-by-step guide

I have been working on a small web application that is distributed in a Docker container, using an nginx image. This application consists of plain html and js code, without any frameworks. Within the JS code, there is access to a remote host via WebSocket ...

Spacing applied to content within a fresh Vue application

Having been assigned the task of developing a Vue page for my team, I am facing an issue with a persistent white border surrounding the entire page. <script setup lang="ts"> </script> <template> <body> <div>&l ...

What could be the reason for encountering TypeScript within the Vue.js source code?

While exploring the vue.js source code, I stumbled upon some unfamiliar syntax that turned out to be TypeScript after further investigation. What baffled me was finding this TypeScript syntax within a ".js" file, when my understanding is that TypeScript ...

Is there a way for me to display a gif similar to 9GAG on my

I'm looking to implement a feature on my website that allows me to pause and play a gif, similar to the functionality on 9gag. Can anyone provide guidance on how I can achieve this? I understand that I need to use both .jpg and .gif files, but my at ...

What is the best way to transfer the http server variable between different layers in node.js without requiring it in a separate file?

I've developed a nodeJS application that involves creating a server in the file server.js. The code looks like this: http.createServer(app).listen(app.get('port'), function (err) { if (err) { console.error(err); } else { ...

What causes the lack of impact on lambda rendering speed despite integrating webpack?

Hey there, I've been working on implementing webpack for a project that involves microservices, Node.js, TypeScript, AWS, and AWS SAM. My main objectives are: Reduce the cold start time of lambda functions. Minimize security vulnerabilities by e ...

looking to display the latest status value in a separate component

I am interested in monitoring when a mutation is called and updates a status. I have created a component that displays the count of database table rows when an API call is made. Below is the store I have implemented: const state = { opportunity: "" } ...

Targeting lightgallery.js on dynamically added elements in Javascript: The solution to dynamically add elements to

I am facing a challenge in targeting dynamically added elements to make them work with lightgallery.js. Take a look at the example below: <div id="animated-thumbs" class="page-divs-middle"> <!-- STATIC EXAMPLE --> ...

Having trouble transferring ASP server control value to a Javascript function

Currently working with ASP.NET 2.0. My Entry Form contains various ASP.NET Server Controls, such as TextBoxes. I want to pass the value of a TextBox on the onBlur event. Here is an example of a TextBox code: <asp:TextBox ID="txtTotalTw" Width="80px" r ...

Tips for determining the minimum value within an array of objects across multiple keys using a single function

I am currently tasked with the challenge of determining the minimum value from an array of objects that contain multiple keys. My ultimate goal is to identify the minimum value among all keys or specific keys within the objects. For instance var users = ...

How can I maintain the consistent speed of the Javascript timer even when the .deck is clicked?

Currently, I'm working on creating a memory card game. In this game, the deck of cards is represented by the class .deck. Strangely enough, every time I click on a card, the timer starts to speed up instead of keeping a consistent pace. How can I tack ...

What is the best way to efficiently integrate Google Analytics and Facebook Pixel into Vue.js dynamically?

I am currently working on a Vue.js project with multiple pages for different customers, such as www.sample.com/shopA, www.sample.com/shopB, www.sample.com/shopC. All of these shops share the same file, and I am looking to implement a way for each customer ...

Tips for displaying a close icon after completing a file upload

I need to implement a feature where once a user uploads a file, a cross sign should be enabled on the file. I attempted to use javascript for this functionality, but it seems to not be working as expected. There are more than 8-10 different file types that ...

Unable to transmit the element identifier information through ajax

Whenever I attempt to pass id data through ajax, I encounter an undefined variable error. The ajax function works smoothly with form data, but the issue arises when trying to extract the id value. Below is the PHP/HTML code snippet: <ul class="sub-men ...

Utilizing Angular and TypeScript: The best approach for managing this situation

I need some guidance on handling asynchronous calls in Angular. Currently, I am invoking two methods from a service in a controller to fetch an object called "categoryInfo." How can I ensure that these methods return the categoryInfo correctly and displa ...

React JS routes function properly only upon being reloaded

Encountering a problem with my reactJS routes where the URL changes in the address bar when clicking on a link, but the component does not render unless I refresh the page. Here is an excerpt from my code: index.js import React, { Component } from &apos ...

Align the number of an Unordered List to the left

Exploring UN-ordered lists in HTML has led me to wonder if it's possible for dynamically generated ul tags to display like this: * Hello * Hi * Bi * Name * Ron * Mat * Cloth * Color * Red When I ...

What is the best way to arrange <div> elements with text inside without moving the text?

I need help figuring out how to stack one or more <div> elements on top of another <div> element while maintaining the text content within each. My issue is illustrated in this fiddle: https://jsfiddle.net/rd268ucy/1/. When attempting to drag o ...

Encountering a CORS issue specifically on the client side of a Next.js application when interacting with an API gateway

I've been struggling with this issue for a week now and can't seem to fully describe it. I have a FastAPI server running as a Lambda connected to API Gateway. https://i.stack.imgur.com/S5Zx9.png Both FastAPI and API Gateway have CORS enabled, b ...

Node.js is receiving an empty body from Postman when using form-data in the request

Even though I've searched extensively, I still have not found a solution to my specific issue despite it being asked before. const express = require("express"); require("dotenv").config({ path: ".env", }); const PORT = ...