A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

How to Select an Element by Name in jQuery
Q:

How to Select an Element by Name in jQuery

0

How to Select an Element by Name in jQuery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the Attribute Selector

You can use the CSS attribute selectors to select an HTML element by name using jQuery. The attribute selectors provide a very flexible and powerful mechanism for selecting elements.

Let's take a look at the following example to see how this works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Select Element by its Name Attribute</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    // Select and style elements on click of the button
    $("button").click(function(){
        // Select inputs whose name attribute value ends with '_name'
        $('input[name$="_name"]').css("border-color", "lime");
        
        // Select input which name attribute value is exactly 'email'
        $('input[name="email"]').css("border-color", "blue");
        
        // Select inputs whose name attribute value starts with 'zip'
        $('input[name^="zip"]').css("border-color", "red");
    });    
});
</script>
</head>
<body>
    <form>
        <p><label>First Name: <input type="text" name="first_name"></label><p>
        <p><label>Last Name: <input type="text" name="last_name"></label><p>        
        <p><label>Email Address: <input type="text" name="email"></label><p>
        <p><label>Zip Code: <input type="text" name="zip_code"></label><p>
        <button type="button">Style Inputs</button>
    </form>
</body>
</html>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now