Enhancing Javascript HREF links

Occasionally, I visit URLs that have page numbers indicated in the HREF. As these page numbers change, keeping track of the last one visited becomes a challenge. Cutting and pasting into a text file is a temporary solution; therefore, I intend to replace it with a local HTML page that can be updated using JavaScript. Below is an example of what I have created:

<script type="application/javascript">
      function update(n,newvalue) {
           var link = document.getElementById('l' + n);
           var href = link.getAttribute('href', 2);
           var textfield = document.getElementById('p' + n);
           var parts = new Array();
           parts = href.split('-');
           parts[1] = 'p' + newvalue;
           textfield.setAttribute('value', newvalue);
           var newhref = parts.join('-');
           link.setAttribute('href',newhref);
       }
</script>
<form>
     <dl>
         <dt><a target="_blank" id="l1" href="http://foobar.org/t5-p8-data.html">Task 5 Data<a></dt>
         <dd>Page: <input type="text" id="p1" value="8" onChange="update(1,this.value)" /> </dd>
     </dl>
</form>
    </html>

After entering a new value in the text field and tracing through with Firebug, it appears to work correctly - the link's href value and the text field's value are changed within the DOM. However, upon exiting the function, they revert back to their original values on the page. What could be causing this issue?

Answer №1

UPDATE:

The issue arises when pressing the enter key in an individual <input> within a form tag, resulting in the form submission. This action triggers a glitch where after entering text, hitting enter causes a disruption, leading to the onchange event being activated and displaying correctly in the debugger. Subsequently, the page reloads (due to the default form action being GET for the current page) smoothly without flickering, which resets the input value.

Resolution 1: Eliminate the unnecessary <form> tags as they serve no purpose in this specific code snippet.

Resolution 2: Implement an onsubmit=return(false) attribute within the <form> tag to prevent unintended form submissions.

Resolution 3: Integrate an additional visible input field within the form structure.

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

Tips for reversing the order of a v-for loop using Vue.js

I am working with an array called "names" that starts off empty names: [] To add elements to this array using the unshift() function, which adds elements to the beginning instead of the end, I do it like this: names.unshift("Leonardo") names.unshift("Vict ...

React Alert: It is important for every child within a list to have a distinct "key" prop, not due to a missing key in the map

My current project involves developing a React 18 application. While working on one of the pages, I encountered the following error: https://i.sstatic.net/9Mk2r.png I am aware that this type of error is often linked to the absence of a unique key in the m ...

Balanced-JS encounters a 404 error

I am currently testing out the code sample provided in the following link: https://github.com/balanced/balanced-js After following the README instructions, I was able to get my local server up and running. However, when I visit the website and try to tok ...

Ways to verify if JSON.parse fails or yields a falsy outcome

Objective: const jsonData = JSON.parse(this.description) ? JSON.parse(this.description) : null When executing the above statement, my aim is to have the ability to parse a value successfully and return null if parsing fails. However, instead of achieving ...

Tips for updating the minimum value to the current page when the button is pressed on the input range

When I press the next button on the current page, I want to update the value in a certain way. https://i.stack.imgur.com/XEiMa.jpg I have written the following code but it is not working as expected: el.delegate(".nextbtn", "click", f ...

Angular 7+ boasts the exciting feature of integrating Promises within Observables

I'm faced with a challenging issue that I can't seem to resolve on my own. During the ngOnInit event, I'm monitoring the URL parameter in search of a specific folder within firebase-storage. Upon retrieval, I compile a list of folders and f ...

retrieve instance variables from a TypeORM entity class

In my typeorm setup, I have a custom entity named User with properties like id, firstName, lastName, and isActive. My goal is to extract these property names from the User class and store them in an array as shown below: ['id', 'firstName&a ...

JavaScript: Generating multiple variables using a for loop?

Is there a way to dynamically create n variables a_1, a_2, a_3 ... a_n, where the value of n is determined during runtime? Attempting to use the following code would not produce the desired outcome: var n = prompt("Enter number of variables?"); for (i= ...

Adjusting size and framing of image upon page load using angular directive

I have created a directive that is responsible for rescaling and cropping an image based on its height and width. angular.module('starter.directives', ['starter.controllers']).directive('styleImage', function () { return { ...

The functionality of Mongoose async promise appears to be malfunctioning

Struggling with using async in conjunction with mongoose. Here's my code snippet : function() { SchemaOne.findById(fooIdOne).exec().then( x => { // Some initial instructions myCollection.foreach( y => { SchemaTwo.findById ...

Highcharts, put a halt to the dynamic spline graph rendering

Utilizing Angular, I successfully implemented a Dynamical Spline Graph by following the guidelines outlined in the HighCharts documentation. An important feature I would like to incorporate is the ability for the chart to pause rendering upon clicking a d ...

Storing various text inputs in a MySQL database

Could anyone please assist me with fixing an issue I'm having with inserting data into a database from the form provided below? Unfortunately, I am unable to get it to work as expected. Here is the complete form: <html> <head> <m ...

Various functions for mouse clicks and touchscreen interactions may result in incorrect functionality when using a touchable screen instead of a mouse

After receiving valuable help from Stackoverflow with the current version, I find myself completely lost trying to solve a new issue. Any advice would be greatly appreciated. The problem arises with a product container that displays parameters on mouse :h ...

Removing the gridlines in a Linechart using Apexcharts

I am experiencing issues with the grid and Nodata options on my Apexchart line chart. noData: { text: none , align: 'center', verticalAlign: 'middle', offsetX: 0, offsetY: 0, style: { color: undefined, fontSize: &apo ...

Display a standard chart using the Chart.js library, along with customizable options

I have successfully generated charts from a database using JSON script. However, I am facing an issue where the chart only displays when I click on an option value. What I want to achieve now is setting a default value option so that when the website ope ...

Error Encountered: Unexpected Identifier in Angular 7 External jQuery Plugin

Struggling to convert a jQuery template to Angular7, I'm facing an issue with loading .js files from the assets folder in the original template to make it functional. Upon starting the application with: ng serve, I encounter the following error in th ...

Transforming a REST API get call into GraphQL

I'm currently using a REST API that accepts a code parameter, searches the database for matches, returns results if the parameter exists, and redirects users to a long_url retrieved from the database. How can I convert this functionality to GraphQL? i ...

The Angular 2 bootstrap function is throwing an error stating that the argument type AppComponent cannot be assigned to the parameter type Type

Presenting my very first Angular 2 application with a simple Hello World example, inspired by the Angular 2 quick start guide. import {Component} from 'angular2/core'; import {bootstrap} from 'angular2/platform/browser'; @Component({ ...

What is causing a single state update when setState is called twice in React?

It seems like I'm making a beginner mistake in React as I am trying to call the "addMessage" function twice within the "add2Messages" function, but it only registers once. I believe this issue might be related to how hooks work in React. How can I mod ...

Dealing with multiple submit buttons in a form with Angular JS: best practices

I'm using AngularJS and I have a form where the user can enter data. At the end of the form I want to have two buttons, one to "save" which will save and go to another page, and another button labeled "save and add another" which will save the form an ...