why are use the prop method in jquery? explain with example.

0 votes
51 views
Ethan Karla asked 28-Jul-2021 in Web Application by Ethan Karla

why are use the prop method in jquery? explain with example.    

1 Answer

1 votes
Ravi Vishwakarma answered 29-Jul-2021 by Ravi Vishwakarma
The prop () method in jQuery is used to get and set the property of an selected element. Such as width, height, color etc.
Syntax:
$(selector).prop (property-name)
 This signature are used to get the property
$(selector).prop (property-name, value)
 And this signature are used for setting the property of selected elements
$(selector).prop (property-name, function (index, currentvalue))
 Also this signature set property and this function action at the end of prop ()
$(selector).prop ({property: value, property: value, ...})
 Also this signature set the multiple properties
Example 
<!DOCTYPE html>  

<html lang='en'>
<head>
  <meta charset='utf-8'>
  <script src='https://code.jquery.com/jquery-1.10.2.js'></script>
<script>
    $(document).ready(function () {
        $('button').click(function () {
            $('img').prop({ width: 300 });
            $('p').text('set image property ( width : 300px )');
        });
    });
</script>
</head>
<body>
<img src='https://source.unsplash.com/1600x900/?tree,water' width='500px' ><br>
    <p>default image property ( width : 500px )</p>
<button>Set the width property of the image</button>
</body>
</html>  

Output : 

why are use the prop method in jquery? explain with example.