Seeking a method to attach information from a div to a hyperlink

As a complete novice in the world of CSS websites, I find myself struggling to accomplish what seems like a simple task. I am working with an automatically generated CSS/JavaScript website where on the "individual" page, a person's name is listed within an h2 tag. My goal is to extract this name and add it to a variable in a script that will then create a hyperlink labeled "View All Media," which when clicked will search a database based on the person's name.

<html>
<script language="javascript" type="text/javascript">
var scrt_var = ' ';
openPage = function() {location.href = "http://my.website.net/all_media.asp?tags="+scrt_var;}
</script>
<a href="javascript:openPage()">View All Media </a>
</html>

The specific tag containing the person's name on the "individual" page is <h2 id='name'></h2>. This name needs to be appended to the hyperlink as a search parameter for the database. Unfortunately, my searches only turned up scripts that modify tags or add hyperlinks, rather than extracting data. I'm hoping to automate the process by pulling the name from the page/source instead of manually entering it into the script.

`<div id='content'>`
`<h1>Individual Details</h1>` 
`<h2 id='name'></h2>`

If I hard-code the name in the script (e.g., var scrt_var = "John Doe";), everything works as expected. But my aim is to dynamically retrieve the name from the page/source and populate the "var" line automatically. This should be a straightforward task, but my lack of knowledge is proving to be a hindrance. Any guidance or thoughts would be greatly appreciated! And please, go easy on me - Thank you! 😊

Answer â„–1

Give this a shot:

let secretVariable = document.getElementById('name').textContent;

Ensure that the document is fully loaded before making the assignment, or include it in your function.

openPage = function() {
    location.href = "http://my.website.net/all_media.asp?tags="+document.getElementById('name').textContent;
}

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

Control the playback of individual HTML5 audio elements using jQuery for seamless user experience

How can I modify the code to ensure that only one HTML5 audio element plays at a time? Currently, all tracks are able to play simultaneously. I am using jQuery for play and pause functionalities. If one track is playing and another is clicked on, how can ...

Issue with Access Denied Error in Internet Explorer 11 when Using AngularJS

Every software development process consists of two essential components. FIRST, the hard work of developing the application. SECOND, the even harder task of making it compatible with the notorious Internet Explorer. Currently, we have an AngularJS (v1.3.1 ...

Is it feasible to utilize jQuery without relying on IDs for selection?

I am currently troubleshooting a code snippet in which jQuery seems to be accessing an element by ID even though the element does not actually have an ID assigned. Here is what I have in the markup: @Html.CheckBoxFor(Model => Model.IsEBSToBeCreated, new ...

alteration of colors in a Chartist chart

Currently, I am working with a chartist graph where the colors are defined at a framework level. However, I need to make changes to the colors for a specific graph without altering the framework level settings. I attempted to create a .less file, but unfor ...

What is the method for obtaining the worldwide location of a vertex on a skinned mesh in Three.js?

In the realm of Three.js, we've recently acquired the ability to determine the global position of a vertex in a non-skinned mesh thanks to insights from this question. Now, the query arises - how can one ascertain the global position of a vertex in a ...

Using onChange input causes the component to re-render multiple times

As I delve into learning React, I've encountered some challenges. Despite thinking that I grasped the concept of controlled components, it appears that there's a misunderstanding on my end. The issue arises after an onChange event where I notice ...

Tweaking react-lottie and SVG dimensions with CSS media queries: A step-by-step guide

I am currently working on a React project that involves displaying Lottie-react animations and SVG's. My main concern is making sure that these illustrations are responsive on my website. The components are stored in a .js file, which I import into m ...

When anchor links (href) are incorporated within a flex layout and flex-direction is set to row, they may not function as

I've designed a website with three horizontal pages, each filling the entire screen. There's a fixed menu with links that scroll to specific pages when clicked. However, users can also scroll horizontally to navigate between screens. If two page ...

Display a portion of the existing URL as a clickable link on the webpage using JavaScript or PHP

If I have a website with the URL and I would like to showcase the image at https://example.com/image.jpg on my site (), what can I do? I attempted the following approach, but it only displays the URL of the image. <p id="image"></p> <scri ...

VueJS is preventing the closure of the modal through jQuery

Within the index.blade.php page, I have the following code snippet: <div id="books_listing" class="container"> <div class="alert alert-success" role="alert"> <p>Book suggestions has been enabled.</p> <span cl ...

Uploading Files Using Ajax to a PHP Class

Having some trouble with my file upload using AJAX to post to an object-oriented PHP class. Here's the AJAX code: var handleUpload = function(event) { event.preventDefault(); event.stopPropagation(); var fileInput = document.getElement ...

What could be causing the Contact anchor element to not function properly?

I've been working on creating a dropdown menu for my API, but I'm having trouble getting the anchor links to function properly. I even attempted changing the "a" element to an onclick call with javascript:void(0) and added a function to open Gmai ...

Guide to customizing the default scrollbar colors in Nextjs

When browsing websites like tailwindcss.com or https://developer.mozilla.org/ in Chrome and Firefox, you may have noticed that they utilize the default scrollbar style but allow users to change its colors through a dark/light mode button. The appearance of ...

React dynamic dropdown selection based on previous dropdown values

I have implemented 3 react input fields that are populating data to a useState Hook <FormControl fullWidth> <InputLabel>Primary Goal</InputLabel> <Select ...

Speedy PHP Pagination

When running a MYSQL query that retrieves approximately 10,000 rows for reporting purposes, my current pagination system loads all the rows initially and then reduces them based on the page limit. This process significantly slows down the load time. In ad ...

Preventing browser auto-complete and hiding form sections with JQuery

Utilizing JQuery, I am able to hide a section of a form based on a radio button question with options "yes" (mapped to 1) and "no" (mapped to 0). When the user selects "yes," the hidden part of the form is revealed for them to fill out. Here is the code r ...

Animation Effects in Opera and Internet Explorer

Just unveiled my latest project, CSSload.net - an awesome tool for generating CSS spinners and bars. The animations are running smoothly on Firefox and Webkit browsers. Curious if anyone has tips on animating elements like this in Opera and IE? ...

Timer for searching webpages using Javascript

I am looking for a way to continuously search a specific webpage for a certain set of characters, even as the text on the page changes. I would like the program to refresh and search every minute without having to keep the webpage open. In addition, once ...

Entering a value into an HTML textbox using Awesomium in VB.NET

This code snippet is used to split text from a listbox: For Each Item As Object In ListBox1.SelectedItems TextBox2.AppendText(Item.ToString + Environment.NewLine) Next Dim str As String = TextBox2.Text D ...

The TypeScript declaration for `gapi.client.storage` is being overlooked

When I call gapi.client.storage.buckets.list(), TypeScript gives me an error saying "Property 'storage' does not exist on type 'typeof client'." This issue is occurring within a Vue.js application where I am utilizing the GAPI library. ...