Retrieving the HTML ID attribute of an anchor link within a form and sending it to the backend server

I am currently working on a project that involves a form containing an anchor link tag with a dynamic ID. I am utilizing this anchor link tag to submit the form via Javascript. However, I am facing difficulty in fetching the ID of the same anchor link tag, which is dynamic, and submitting it to the backend server.

Here is the form structure:

<form method="POST" action="#" id="rsmForm">
    <!-- Token field-->
    <input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">  

     <a href="#demo{{$i}}" id="{{ $a['id'] }}" name="asmID" data-value="{{ $a['id'] }}" onclick="document.getElementById('rsmForm').submit();"> + ASM ({{ $a['id'] }}) </a>
</form>

Answer №1

When handling the onclick event, retrieve the href attribute value of the anchor tag:

function onClickAnchor(e) {
  const currentHref = e.currentTarget.getAttribute('href');
  console.log(currentHref);
  // document.getElementById('rsmForm').submit();  <=== uncomment it
}
<form method="POST" action="#" id="rsmForm">
    <!-- Token field-->
    <input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">  

     <a href="#demo{{$i}}" id="{{ $a['id'] }}" name="asmID" data-value="{{ $a['id'] }}" onclick="onClickAnchor(event)"> + ASM ({{ $a['id'] }}) </a>
</form>

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

conditional statement for manipulating data in javascript/html

I am working on appending results in an object as options in a datalist dropdown. While it is functioning correctly, the issue arises when not all elements have a specific level in the object consistently, impacting which results are added to the list. $( ...

JavaScript JSON function failing to show

I apologize for my limited knowledge on this matter... We are currently facing an issue with our website's pricing page. The dynamic calculator, which should display the price based on user input of size width and quantity, is not functioning as expe ...

algorithm for selecting the greatest value

I'm attempting to create a function that identifies the largest number in an array, but for some reason, I'm only able to retrieve the first number in the array. function findLargest(numbers) { var bigNum = 1; for(i = 0; i < numbers.len ...

activate the button once valid input has been provided

How can I enable a button based on the amount entered? Let's say there is a minimum of 100 and a maximum of 200. If the user enters an amount below 100, display an error message and keep the button disabled. If the user enters an amount above 200, ...

What is the best way to decrease the border width of a chartjs doughnut chart?

I have a vision for my chart based on the mockup: However, here is what I've been able to achieve using chartjs so far: This is the code I'm working with: datasets: [ { data: [3, 8, 13, 9, 2], backgroun ...

The back-to-top feature is malfunctioning in the new Bootstrap 4 update

I've been working on adding a back-to-top button to my website using JavaScript in Bootstrap 4 with the Font Awesome icon set. It was functioning perfectly until I made some changes to the site, and now it's not working anymore. I'm pretty n ...

Switch from Index.html to Index.html#section1

Currently, I am working on a website and sending someone a draft for review. However, the Home screen is designed as a 'a href' link with "#home". The issue arises when the website opens from my computer; it goes to ...../Index.html instead of .. ...

Using Node.js to convert a file name to a timestamp

I need to change a filename into a timestamp in my Laravel application. let test = "/2020-05-16_11-17-47_UTC_1.jpg" I attempted using split and join to replace the -, but I don't believe it's the most efficient method. The desired output should ...

Finding the occurrences of elements in an array using JavaScript

While browsing Stack Overflow, I stumbled upon a question that has yet to be answered: How can I count the occurrences of elements in a specific array using JavaScript?. let array = [6, 1, 5, 1, 1, 8, 2, 4, 6, 0] // Elements in array getOccurrence(array) ...

Syntax for retrieving response with jQuery AJAX

Currently, I am in the process of working on an AJAX request that triggers a URL when a specific button is clicked. The fundamental aspects are running smoothly, ensuring that the GET request is activated towards the URL upon button click. I have also cros ...

Dynamic backdrop featuring engaging content

I am currently working on a WordPress website that features a dynamic background. While I have successfully implemented movement to the background, the content on the site remains static and unaffected by scrolling. The background does not move alongside t ...

What are the drawbacks of automating the process of generating charts to track time spent on webpages?

!RESOLVED! I am looking to automatically generate charts based on users and their time spent on different pages of a website. I have a file named "log.xml" where I store user information (customers), visited pages, dates, and the time they spent; once I ...

Passing the value of a radio button to a component in React: a comprehensive guide

I've been struggling to figure out how to pass the value of a selected radio button to another component in React. Currently, I'm exploring the Star Wars API and attempting to create a basic search interface where users can retrieve information a ...

Storing duplicate code in multiple cache files using ReactJS ServiceWorker

I am currently working on integrating a serviceworker into an existing React application that has the following filesystem layout: Filesystem The initialization code is stored in the public folder, while the main code resides in the src folder. In my serv ...

AngularJS ng-model not refreshing

One of the features in my application is a Font Awesome icon picker that allows employees to easily access different icons without having to search for their codes online. However, I am facing an issue where clicking on an icon does not update the ng-mode ...

Emulating ArrowRight event using React Testing Library

I am facing a challenge with the React-Testing-Library. Within my application, I have two components called HeroesScreen and HeroesDiamondBar. The HeroesDiamondBar component is rendered inside the HeroesScreen component once the currentStep state variable ...

Bing Map problem: p_elSource.attachEvent is throwing an error

I am encountering a significant issue with Bing Maps. The API link I am using is: Initially, I am seeing an error in firebug that looks like this: this.CreditsFor=function(a,i,j,h) { var e=[]; if(a!="undefined"&&a!=null&&typeof m_ta ...

unable to connect to the web service using webview

When attempting to call a web service or display an alert, I am encountering some issues: Here is the code from my activity: mWebView = (WebView)findViewById(R.id.webViewRootAciviy); mWebView.setWebViewClient(new WebViewClient()); mWebView.setWebChromeCl ...

Unable to find the matching closing parenthesis ')" before reaching the end

I've been struggling to create a regular expression that can identify strings like the ones below: var p1=@VAL([Test1Q1].[Bandwidth]) var p2=@VAL([Test1Q1].[Usages (KB)]) The criteria is to find strings starting with @VAL( and ending before the firs ...

Regular expression for eliminating the local path from a website address

I am facing a scenario where different formats of relative paths can occur, such as: const URL1 = "./hello-world" const URL2 = "../hello-world" const URL3 = "../../hello-world" const URL4 = "../../../hello-world" con ...