jQuery Selector [attribute^=value] by attribute start

Introduction

The [attribute^=value] selector selects elements by attribute starting value.

$("[attribute^='value']")
Parameter OptionalDescription
attribute Required.the attribute to find
value Required. the string the value should begin with

Select all <input> elements with a name attribute that starts with "flag":

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("input[name^='flag']").css("background-color", "yellow");});
</script>//  ww  w.  ja  va 2 s. com
</head>
<body>

<input name="flagMark" type="text" value="CSS">
<input name="flag" type="text" value="HTML">
<input name="myflag" type="text" value="Java">
<input name="anotherflag" type="text" value="Javascript">
</body>
</html>



PreviousNext

Related