Greg
0
Q:

jquery set attribute

//<div id="myElementID" data-myvalue="37"></div>
var a = $('#myElementID').data('myvalue'); //get myvalue
$('#myElementID').data('myvalue',38); //set myvalue
3
$(this).attr("width", "500");
6

$("button").click(function(){

  alert($("#w3s").attr("href"));

});
 
4
$input.attr("attributeName", "attributeValue");
4

$("button").click(function(){
  $("img").attr("width","500");
 });
 
1
$( some_item ).attr( "id", "some-id" );
// for setting multiple attributes, it's similar to the css() property. However, quotes are not always required for attr()
$( some_item ).attr({
  id: "some-id",
  // or others.
  title: "Opens in a new window",
  // attributes which contain dash(-), should be covered in quotes.
  "data-value": "internal link"
});
3
<!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>
1

New to Communities?

Join the community