individual elements displayed sequentially within the same block with variable naming

My code caters to both mobile and desktop versions, with the same elements in the FORM. However, only one block of code is visible at a time depending on the screen size, with one set to display: none.

<div id="desktop">
  <div id="content">
    <form method="post" name="form_name_desktop" id="id_form_desktop">
      <input type="email" name="name_email_desktop" id="id_email_desktop">
      <input type="hidden" name="hidden_input_desktop" value="submit_sub_desktop">
      <a href="#" id="btnSub_desktop">SAVE</a>
    </form>
  </div>
</div>


<div id="mobile">
  <div id="content">
    <form method="post" name="form_name_mobile" id="id_form_mobile">
      <input type="email" name="name_email_mobile" id="id_email_mobile">
      <input type="hidden" name="hidden_input_mobile" value="submit_sub_mobile">
      <a href="#" id="btnSub_mobile">SAVE</a>
    </form>
  </div>
</div>

//some JQ code
    $("#btnSub").click(function(e) {
        e.preventDefault();
        if ($("#id_email").val() == "") {}
    });

//some PHP code
  if (isset($_POST["hidden_input"]) == "submit_sub") {}

Although the above code appears identical for both desktop and mobile views, the elements are actually arranged differently. My question is: should I use the same naming convention for IDs and NAMES or should I differentiate them even though they are displayed individually?

The best approach would be to rewrite the example above with correct naming conventions.

I'm asking this question because I'm encountering issues running my existing code. I am unsure whether I need to make wholesale changes or just adjust a few specific aspects. I want to understand the main rules to follow in these situations.

Answer №1

When dealing with data in varying formats, it is possible to use identical names for sending the information. However, when accessing these elements in your JavaScript code, it is recommended to use classes instead of repeating the same names.

It's important to remember that IDs should be exclusive to each page, making them perfect candidates for representing unique elements on a webpage. In cases where multiple elements share similar characteristics, classes provide a more versatile solution.

:)

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

Issue encountered while adding platform in Cordova version 8.1.2

Attempting to set up a new Cordova project. The version of Cordova being used is 8.1.2 ([email protected]). I ran the cordova create command and it was successful. However, when trying to add the Android platform with the command cordova platform add ...

The attribute of the Angular div tag that lacks an equal sign

Apologies if this question has been asked before. I've noticed in some people's code that they use the following syntax: <div ui-grid="myUIGrid" ui-grid-selection ui-grid-resize-columns class="grid" /> Can someone explain what ui-grid-sel ...

Node's Object.prototype function returns an empty object

When I run Object.prototype in the browser console, I see all the properties and methods within it. However, when I do the same thing in the NodeJS terminal, I get an empty object {}. Can someone explain why this difference occurs? Attached are screenshots ...

Creating an ngInclude directive on the fly: A step-by-step guide

Whenever I insert this into my HTML, everything functions correctly: <div ng-include="'my-template.html'"></div> However, when attempting to dynamically create that ngInclude directive using jQuery (after the DOM has loaded), it fai ...

How can I execute JavaScript code within Aptana Studio 3 IDE?

After creating a Test.js file, I added two lines of JavaScript code: var x = 5; console.log("The answer is: " + x); The desired output would be: "The answer is: 5" I am curious if there's a way to view this outcome in the Aptana Scripting console, ...

When using the setTimeout function to update the state, React useContext appears to be ineffective

As a newcomer to React, I have a question about refreshing the score in my card game within a forEach loop using setTimeout. While the state appears to update correctly, the DOM (Component overarching) does not reflect these changes. export function Refill ...

H1 and span elements experiencing abnormal size discrepancies

Check out this code snippet: <h1><a href="">Windows App Store<br /><span class="smallSubText">applications</span></a></h1> and here's the CSS styling: #windowsStoreApplications { float: right; width ...

Eliminate any HTML content using Jsoup while retaining the text lines

I am working with a String that contains e-mail content, and my goal is to eliminate all HTML coding from this String. This is the current code I have: public static String html2text(String html) { Document document = Jsoup.parse(html); document = ...

JavaScript: An object containing a unified handler for all method invocations

Let me explain further. Math.add(2, 2) //4 Math.multiply(4, 9) //36 Whenever a method in the Math object is invoked, it triggers a central processor that interprets the function name to determine its action. Can an object execute a default function when ...

Unlocking the Secrets of Laravel Blade Integration in a Script File

I am attempting to create a customized store locator application using the guidance provided in this useful tutorial on Google Maps and Laravel 5. While going through various queries related to integrating Google Maps with Laravel, I came across these info ...

Tips for concealing broken images in jQuery/JavaScript when generating dynamic content

Is there a way to hide the broken images in this dynamically generated HTML code using JavaScript? Unfortunately, I don't have access to the source code itself. I'm new to JQuery and could really use some assistance. Below is the snippet of the ...

There is a lack of communication between the tomcat application and the database

I am currently working on a web application that is stored in a war file and runs with Tomcat. Initially, I start my MySQL 5.7 database, followed by running a Spring Boot project created with the necessary files. After completing these steps, I deploy my ...

Showing canvas lines while dragging (using only plain JavaScript, with React.JS if needed)

Is there a way to add lines with two clicks and have them visible while moving the mouse? The line should only be drawn when clicking the left mouse button. Any suggestions on how I can modify my code to achieve this functionality? Currently, the lines are ...

Why won't my redux application store the results of an asynchronous API request using redux-thunk's caching mechanism?

I am new to using Redux in my project. Currently, I am developing an application that displays a list of soccer leagues in each country. The process involves fetching a list of countries first, then using the country names to fetch the soccer leagues. Not ...

What is the best way to embed a webpage into another webpage using frames?

I need help embedding an iframe on my website, , to display the items I am selling on eBay. This is my first time attempting to create an iframe. ...

Looking for a jQuery Gantt Chart that includes a Treeview, Zoom functionality, and editing capabilities?

I am in need of integrating a dynamic Gantt Chart tool into a room booking solution using jQuery. The Gantt chart should be highly interactive, allowing for drag-and-drop functionality, a tree view on the left to group tasks, and a zoom feature for adjusti ...

npm Error: The installation script for [email protected] has encountered a failure

I encountered an error while attempting to execute "ionic start MyApp" in the Windows terminal. This is a new machine and I have already reinstalled nodejs and ionic, but the same error persists. Can someone lend a hand? npm WARN tar invalid entry & ...

Tips for extracting values from a JSON object

I have been attempting to retrieve the value from an array without success. Here is the current array: [{0: {value: 2, label: "ARKANSAS"}}] When I try to use JSON.parse(object), I encounter the error VM20617: 1 Uncaught SyntaxError: Unexpected ...

Utilizing JSON data in JavaScript to populate a datatable

After receiving a JSON result from an AJAX jQuery call, I stored it in a variable and attempted to insert the JSON data into a datatable. However, instead of inserting the data from the variable as intended, the datatable is currently populated with JSON d ...

Making changes to a list by adding or removing items, as well as adjusting the overall total

I'm in the process of creating a unique feature similar to a shopping cart, but instead of adding items to the cart, I'm adding them to a shortlist. My goal is to add the item to the shortlist, update the count on the shortlist, and then display ...