उदाहरण
रिटर्न स्टेटमेंट का उपयोग करके फंक्शन को वेरिएबल में असाइन करें।
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.demo { background: skyblue; height: 20px; width: 75%; margin: 10px; padding: 10px; }
</style>
</head>
<body>
<div class="demo" data-st="John">John Smith</div>
<div class="demo" data-st="David">David Miller</div>
<div class="demo" data-st="Adam">Adam Smith</div>
<script>
var studentNames = function() {
var names = [];
$(".demo").each(function() {
names.push($(this).data('st'));
});
return names.join();
};
console.log(studentNames());
</script>
</body>
</html>
फ़ाइल सेव करें और Live Server से खोलें।
आउटपुट
कंसोल में सभी डेटा एट्रिब्यूट वैल्यूज़ कॉमा-सेपरेटेड दिखाई देंगी।