How to discover a CSS Selector name?

Joined
Aug 16, 2022
Messages
52
Reaction score
2
Hi Everybody,

I'm using the "required" argument within each of several inputs on my form.
When the form is submitted, an empty input will display a message like this,
and then sorta pause so that a visitor can reply properly. Then the visitor is
expected to fill the field and press the submit <button> again.

Screenshot 2023-09-12 091632.png


If I want to modify that that PopUp, (CSS: ????? { border-color: #FF0000} I need to know the CSS Selector name.

  • Is there a way for me to discover what the CSS Selector name is, that my browser is following?
  • Is there a way for me to read the details a browser is going to follow, once a CSS Selector name is known?
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
Just add the attribute 'required' to the form field and the form will not submit.
From w3schools:
Code:
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Validation</h2>

<form action="/action_page.php" method="post">
  <input type="text" name="fname" required>
  <input type="submit" value="Submit">
</form>

<p>If you click submit, without filling out the text field,
your browser will display an error message.</p>

</body>
</html>

Here are the sectors for css: https://www.w3schools.com/cssref/css_selectors.php

You should study code at this site!!!
 
Joined
Aug 16, 2022
Messages
52
Reaction score
2
I get what you mean, BigDady. What I'm trying to do is modify how that PopoUp message appears, but to do that I need to know what the selector name is.
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
If you want to customize the appearance of the message or pop-up that appears when you try to submit a form with an empty required field, this is not available for styling using regular CSS style sheets. The appearance of this message is controlled by the web browser and is part of its default behavior related to form validation.
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
but ... check this
[ on-line ]
HTML:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">

    <style>
      input {
        display: block;
        margin: .5rem 0;
      }
      input[data-required] {
        background-color: yellow;
      }
      .custom-validation-message {
        display: none;
        width: 50%;
        background-color: red;
        color: white;
        padding: 1rem;
        border-radius: .25rem;
      }
      button {
        margin-top: 1rem;
      }
    </style>
  </head>
  <body>
    <form>
      <input type="text">

      <input type="text" data-required>
      <div class="custom-validation-message">
        This field is required.
      </div>

      <input type="text" data-required>
      <div class="custom-validation-message">
        This field is required.
      </div>

      <input type="text">

      <button type="submit">Submit</button>
    </form>

    <script>
      const form = document.querySelector('form');

      form.addEventListener('submit', function(e) {
        let required = 0;
        for (const element of form)
          if (element.matches('input[data-required]'))
            if (!element.value.trim()) {
              element.nextElementSibling.style.display = 'block';
              required++;
            } else
              element.nextElementSibling.style.display = '';
           
        if (required > 0 ) e.preventDefault();
      });
    </script>
  </body>
</html>
 
Last edited:
Joined
Aug 16, 2022
Messages
52
Reaction score
2
but ... check this
[ on-line ]
HTML:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">

    <style>
      input {
        display: block;
        margin: .5rem 0;
      }
      input[data-required] {
        background-color: yellow;
      }
      .custom-validation-message {
        display: none;
        width: 50%;
        background-color: red;
        color: white;
        padding: 1rem;
        border-radius: .25rem;
      }
      button {
        margin-top: 1rem;
      }
    </style>
  </head>
  <body>
    <form>
      <input type="text">

      <input type="text" data-required>
      <div class="custom-validation-message">
        This field is required.
      </div>

      <input type="text" data-required>
      <div class="custom-validation-message">
        This field is required.
      </div>

      <input type="text">

      <button type="submit">Submit</button>
    </form>

    <script>
      const form = document.querySelector('form');

      form.addEventListener('submit', function(e) {
        let required = 0;
        for (const element of form)
          if (element.matches('input[data-required]'))
            if (!element.value.trim()) {
              element.nextElementSibling.style.display = 'block';
              required++;
            } else
              element.nextElementSibling.style.display = '';
          
        if (required > 0 ) e.preventDefault();
      });
    </script>
  </body>
</html>
I like that, VBService. I've read the lines and studied the code but the "document.xxxx('??')" kinda stuff is brand new to me. It looks worthy of more study. Is this a JavaScript script or XML script?
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
It looks worthy of more study
then check this: HTML DOM Elements

Is this a JavaScript script or XML script?
actually ... no, this is to xml script,

but, ... you can operate on XML using JavaScript. JavaScript provides various ways to work with XML data, and two commonly used methods are:
  • DOM (Document Object Model): JavaScript can manipulate XML documents using the DOM API. You can load an XML document into a DOM tree and then traverse and manipulate the XML elements using JavaScript. Here's a basic example of how you can work with XML using DOM:
    JavaScript:
    // Create a new XMLHttpRequest object to load the XML datavar xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState === 4 && this.status === 200) {
            // Load the XML data
            var xmlDoc = xhttp.responseXML;
    
            // Access XML elements and attributes
            var title = xmlDoc.getElementsByTagName("title")[0].textContent;
            var author = xmlDoc.getElementsByTagName("author")[0].textContent;
    
            // Manipulate XML data as needed
            console.log("Title: " + title);
            console.log("Author: " + author);
        }
    };
    
    // Open and send the request to load the XML file
    xhttp.open("GET", "example.xml", true);
    xhttp.send();

  • XPath: XPath is a language for navigating XML documents. You can use JavaScript libraries like xpath or built-in browser XPath support to query XML data using XPath expressions:

    JavaScript:
    // Load the XML data as a string
    var xmlString = '<bookstore><book><title>Book 1</title><author>Author 1</author></book></bookstore>';
    var xmlDoc = new DOMParser().parseFromString(xmlString, "text/xml");
    
    // Use XPath to query XML data
    var title = xmlDoc.evaluate("/bookstore/book/title", xmlDoc, null, XPathResult.STRING_TYPE, null).stringValue;
    var author = xmlDoc.evaluate("/bookstore/book/author", xmlDoc, null, XPathResult.STRING_TYPE, null).stringValue;
    
    console.log("Title: " + title);
    console.log("Author: " + author);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top