jquery attribute code example
Example 1: jquery add attribute
$('#someid').attr('name', 'value');
Example 2: jquery attribute selector
Syntax
$("[attribute|='value']")
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p[title|='Tomorrow']").css("background-color", "yellow");});
</script>
</head>
<body>
<p title="Tomorrow">This is a paragraph.</p>
<p title="tomorrow">This is a paragraph.</p>
<p title="Tom">This is a paragraph.</p>
<p title="See You Tomorrow">This is a paragraph.</p>
<p title="Tomorrow-the day after today">This is a paragraph.</p>
<p>This selector selects all elements with a title attribute value equal to 'Tomorrow', or starting with 'Tomorrow' followed by a hyphen.</p>
</body>
</html>
Example 3: get attribute value jquery
var some_var = $( some_jquery_selector ).attr( 'some_attribute_name' );
Example 4: jquery attr
$( some_item ).attr( "id", "some-id" );
$( some_item ).attr({
id: "some-id",
title: "Opens in a new window",
"data-value": "internal link"
});
Example 5: jquery add attribute
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Adding Attribute to HTML Element</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$(".add-attr").click(function(){
$('input[type="checkbox"]').attr("checked", "checked");
});
});
</script>
</head>
<body>
<button type="button" class="add-attr">Select Checkbox</button>
<input type="checkbox">
</body>
</html>