Tips for appending the id of an active element to a URL

My goal was to include the id value of the active element in a URL and then redirect to that URL with a button click.

HTML

<div class="icon" tabindex="0" id="company">
    <img src="company.png">
</div>
<button type="submit" onclick="myFunction()">jump</button>

Javascript

function myFunction(){
   var x = document.activeElement.id;
   location.href = "apps-online.html?page=" + x;
}

When clicking the button, my expectation was to be redirected to:

"apps-online.html?page=company"

However, the url ended up being:

"apps-online.html?page="

I'm puzzled as to why the value of x wasn't added to the url.


Hello everyone. I have now realized why this issue occurred. Each time the button is clicked, it becomes the last active element.

Is there a way to solve this and achieve my desired result?

Answer №1

Make sure to verify the value of x to see if it is empty. If x is empty, then the URL will have no value. Try troubleshooting the code or utilize console.log.

function myFunction(){
   var x = document.activeElement.id;
   console.log(x);
   location.href = "apps-online.html?page=" + x;
}

This will display the value of x in the console for verification purposes.

Answer №2

Looks like your button is missing an id.

Add id="company" to the button element.

<div class="icon" tabindex="0">
  <img src="company.png">
</div>
<button id="company" type="submit" onclick="myFunction()">jump</button>

Answer №3

When you press the button, the highlighted element is the button itself. Let's say you have multiple divs and you want to pass the id of the selected element. There needs to be a way to transfer the id to the function.

Check out the example below for one possible solution.

HTML

<div class="icon" tabindex="0" id="company">
    <img src="company.png">
</div>
<div class="icon" tabindex="1" id="something">
    <img src="something.png">
</div>
<button type="submit" onclick="myFunction()">jump</button>

JS

let tabID = null;
let icons = document.getElementsByClassName('icon');

for (icon of icons) 
{
    icon.onclick = function() 
    {
        tabID = document.activeElement.id;
    }
}

function myFunction()
{
    window.open("apps-online.html?page=" + tabID);
}

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

Using AngularJS to manage cookies along with arrays

I am passing my data in this way $cookies.putObject("currentLocation,values,allLocList", obj, vm.tempData, vm.allLocationList); The objects obj and vm.tempData are being sent as objects, while vm.allLocationList is an array that contains a JSON object. W ...

Adjusting the size of the jQuery window in Google Chrome

I'm encountering an issue with resizing a menu. The code I have works fine on all browsers except Chrome. Describing this problem in words is tough, so I'll provide some screenshots to better illustrate the issue. This is how my menu appears in ...

How to align an unordered list vertically in the center using Bootstrap?

Trying to figure out why the ul within this parent div is not centered vertically. Here is a screenshot of the issue: https://i.sstatic.net/ZOc9M.png <div className=" d-flex align-items-center bg-primary "> <ul ...

Analyzing all the <option> elements within a <select> tag with jQuery

I have a WordPress plugin in development and I am currently facing an issue with comparing user-input from an <input> element to a set of <option> elements within a <select> element. Here is my current approach: $('button#test_bu ...

Is there a way to run a node script from any location in the command line similar to how Angular's "

Currently, I am developing a node module that performs certain functions. I want to create a command similar to Angular's ng command. However, I am facing compatibility issues with Windows and Linux operating systems. Despite my attempts to modify the ...

What is the best way to iterate through an array of objects in React and JavaScript, adding a new property to each object in order to generate a new array

Greetings, I am currently dealing with an array of objects structured as follows: const arr_obj = [ { children: [{}], type: "type1", updated: "somevalue", }, { children: [{}], type: ...

ng-repeat and $scope problem

I am having an issue with my page where I display 3 images in a row using Ng-repeat. When I click on any image, it only shows the first image that was displayed in that particular row. Template: <div id="galscrolldiv" class="row" ng-repeat="image in i ...

Creating a flexible image layout within a container with relative positioning

I attempted to create a basic slideshow with images sliding from right to left as an animation: let slides = document.querySelectorAll('.img-container'); let l = slides.length; let i = 0; setInterval(function(){ i = (i + 1) % l; if(i == 0){ f ...

Creating a complete webpage using HTML

Is there a method to load an HTML file and execute the embedded javascript to create a complete HTML page programmatically similar to how browsers do? Edit 1 - I am working on an application where I need to extract data from an HTML page on a remote websi ...

Display a loading dialog when an AJAX request is made

I am working on a Grails application and I would like to implement a visual indicator, such as a modal dialog, to show when an AJAX request is in progress. Currently, I rely on JQuery for all my AJAX requests. While they are triggered using Grails tags at ...

On certain websites, the footer may appear distorted or incorrectly displayed when viewed in the IE 9 web browser

Currently experiencing an issue with the display of the footer in IE9. On some pages, the footer is not properly fixed and overlaps the middle of the content. However, this problem does not occur when using other web browsers. Here is the code that I am c ...

The issue of passing state in React Router v4 Redirect unresolved

I have a specific private route, /something, that I only want to be accessible when logged in. I've used Redirect with the state parameter set, however, when I try to access it at the destination, location.state is showing as undefined. Here is how I ...

vertically centering a div specifically on laptops

What is the best way to vertically center a div on lap-tops? (...) body {padding: 4% 16%;} body {top:0px; left:0px; bottom:0px; right:0px;} body {border: 12px solid darkred; border-style: double;} body {background-color: #FAFCB4;} p {font-size: 80%; text- ...

Tips for looping through nested JSON data in Google Sheets

In my attempt to iterate through the nested "line_items" element in a JSON response triggered by a post event, I aim to populate a Google Sheets spreadsheet using Google Apps Script. My objective is to write the elements within each "line_item" into new ro ...

Retrieve the key{index} property from the .map method in React for the element that was just clicked

I am facing an issue with my code const Component = () => { const [Clicked, setClicked] = useState(false); const handleClick = (prop, e) => { console.log(prop); } return ( <div> {arrayCard.map((item, i) => { ...

Updating an array in Vue.js without the need to reload all the data

Recently delving into Vue.js and JavaScript, I'm seeking guidance on updating an array (on Add/Edit/Delete) without having to reload all the data. The goal is to load all data only when initially opening the page with Addresses. Take a look at my cod ...

Incorporating an HTML/Javascript game into a reactJS website: A step-by-step

After developing a game using plain javascript and HTML along with a few JS libraries, I find myself inquiring about the process of integrating this game into my ReactJS website. Typically, the game is initiated by opening the index.html file located with ...

Display the loading status while fetching data

I recently implemented a code snippet that allows users to download files to cache for offline viewing. You can check it out here: While the code works well, I've noticed that for larger files, the loading process can be a bit slow. I am looking for ...

How come the inclusion of a DOCTYPE tag impacts the styling of my CSS?

I am puzzled why the code below renders correctly without any issues until I add a doctype declaration: <body style="margin:0; padding:0; background-color:#eeeeee"></body> <div id="HeaderContainer" style="background-color:#eeeeee; color:Bl ...

Exploring ways to retrieve items from a JSON array in Node.js, ideally avoiding traditional iteration methods

In my current scenario, the JSON response I receive looks like this... {items:[ {itemId:1,isRight:0}, {itemId:2,isRight:1}, {itemId:3,isRight:0} ]} My goal is to achieve something similar to the following (pseudo code) var arrayFound = obj.items.F ...