Scroll to a new section of the page if the specified id or name cannot be found

Currently, I am dealing with an exceptionally lengthy textual content page.

<p class="parahead">401. Heading </p>
<p>other html content</p>
<p class="c2">some more data</P>
...
<p class="parahead">402. Another heading </p>
<p>other html content</p>
...

Unfortunately, it is not feasible to assign an Id or name property to each of these paragraph elements. However, jQuery is present on the page. My query is whether it is achievable to navigate within the page based on the content of the paragraphs. I prefer not to add any additional tags dynamically as this may impede the page's loading speed.

update: To provide further insights into the issue - I have two HTML files, referred to as A and B. File A serves as the source while B contains explanations about A. During the scroll event in A, I can extract the content/id of the current paragraph within the viewport. Upon clicking a button within the wrapper around A, the user should be directed to the relevant section in B (comprising explanations related to A).

Answer №1

Here is one way you could approach it:

//When the paragraph is clicked
$('.parahead').click( function(){

//Get the number inside the paragraph
var page = $(this).text().split(".");

//Go to a specific page like 401.html
window.location.replace("./" + page[0] + ".html");
});

Does this solution meet your requirements?

Answer №2

On the contrary,

My goal is to refrain from inserting any additional tags to this html on the fly in order to maintain optimal page load speed

Despite my intentions, I end up including a dynamic element during the page load process.

$(function(){
        addTags();
        function addTags(){
        $('.parahead').each(function (){
            var id = parseInt($(this).text());
            console.log(id);
            $(this).after("<span name='parahead" + id +"'></span>");
        });
        }
});

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

Exploring nested elements with JQuery

When utilizing Jquery... I aim to search for a specific element within a parent div, regardless of its nested position within the parent element. After finding these elements, I will then search through them and based on their values, enable or disable o ...

Selecting an option on JQuery UI Autocomplete replaces the displayed label with its corresponding value

Currently, I am utilizing JQuery UI Autocomplete along with an ASP.NET Textbox to display both the label and the value. The following snippet showcases how I have mapped the data fetched from an ajax call: success: function (data) { response($.map(dat ...

Using Background Images in Emails with HTML

Struggling to add a background image with overlaid text? I've attempted multiple methods without success. Can someone provide a foolproof solution for achieving this? Thank you. ...

Adding style tags dynamically to a component and then encapsulating the styles again (using Angular)

Currently, I am in the process of upgrading from AngularJS to Angular. Our app is currently a hybrid one being bundled up with webpack instead of just angular cli. Due to this setup, we have encountered issues where our css or scss files are not correctly ...

Refreshing a webpage is not necessary when using a JQuery form within a div

I need help with some code I have. In my index.html file, there is a div: <div id="logindiv"> <?php require_once('login.php'); ?> </div> This div is utilized with jQuery: <script type="text/javascript"> $(document ...

Manipulating data in textarea using datatables

I am attempting to implement functionality where the clicked row in a datatables is added to a textarea. If the same row is clicked again, the data should be searched in the textarea and removed if found (select/deselect). When I select one row followed b ...

Concealing the rear navigation button within the material carousel

I have a material css carousel, and I am trying to hide the back button on the first slide. I attempted to use the code below from a previous post The following code snippet prevents the user from looping through the carousel indefinitely. Stop looping i ...

Interactive HTML and PHP form designed to compute and display the result of a straightforward mathematical equation

I'm looking to add a dynamic HTML/php form on my webpage that can solve the following formula instantly without any page refresh. I am confused about the best approach and the code required to make it happen. The formula I want to implement is as fol ...

Ensure to verify the dimensions and size of the image prior to uploading

Is there a way to check the dimensions of an image using this function? I want to verify it before uploading... $("#LINK_UPLOAD_PHOTO").submit(function () { var form = $(this); form.ajaxSubmit({ url: SITE_URL + 'functions/_app/execute ...

Highcharts displaying black color after AJAX refresh

Struggling to implement real-time data visualization using Highcharts, I found myself tired of sifting through documentation and feeling confused. My solution involves using AJAX refresh. When the page initially reloads, my chart renders properly. However, ...

Display dynamic text from an input field in another div along with a checkbox

In the input field below, you can type something and then click the Add button. What will happen is that the text entered in the input field will be appended with a checkbox inside a div with the class .new-option-content. For a live example, check out th ...

Trouble encountered with card flip style login form in Vue.js, as the card is not maintaining its position properly during transition animations

Need help styling a login/signup component in Vue.js where flipping between the two cards isn't working smoothly. The transition is causing one card to move down and right while the other comes in from the bottom right. It's hard to explain the i ...

How can JavaScript onClick function receive both the name and value?

My current challenge involves a function designed to disable a group of checkboxes if they are not checked. Originally, this function was set to work onClick(), with one argument being passed from the checkbox element. Now, I need this function to be trigg ...

Can someone help me troubleshoot why my multi-step form is not functioning properly?

I've been working on a multi-phase form, but it's not behaving as expected. I've tried different code variations, but for some reason, it's not functioning properly. After spending two days on it, I'm starting to feel a bit frustra ...

Using JQuery to dynamically set dropdown option values from a JSON object

I have an HTML code snippet: $.ajax({ type: "POST", url: "hanca/hanca_crud.php", dataType: 'json', data: { id_hanca: id_hanca, type: "detail_hanca" }, //detail_hanca success: function(data) { var teks = ""; $.each( ...

Tips for preventing images from stretching the width and height of my HTML

I am facing an issue with my SVG files positioned as "absolute" on my page. When they are close to the corners of the page, they end up expanding the width of my Container element (using Material UI React). I have tried setting "maxWidth":"100vw" on the ...

Utilize jQuery to serialize the HTML input elements that are dynamically generated outside of the form

A proof of concept is in progress for an application that involves the generation of HTML elements based on user-configured fields. Here is a sample configuration: // Customer SAM $response = array( array ( NAME => CUSTOMER, TYPE = ...

The input field in Angular dynamically populates values into multiple instances of the same text

I am facing an issue with my comment inputs where whatever I type in one input is automatically populated in all other inputs. How can I ensure that the text input value only appears in the input box that I am typing into? Below is a snippet of the templa ...

Exploring the Combination of jQuery UI and ASP.NET

Right now, I am diving into the world of jQuery and struggling with a key concept. Working on web applications with ASP.NET means I need to stay in that realm, but integrating jQuery UIs is proving to be a challenge for me. My main question is: How can I ...

Avoiding line breaks for a div positioned on the right within a flexible container

My current design consists of a red bar inside a container that is positioned between two black boxes. The size of the boxes remains fixed, while the red bar and container are both based on percentages. I am aiming to decrease the size of both the contain ...