read data- from jquery object code example

Example 1: jquery data attribute

data attribute in jquery 

For setting attribute data to the element
Html
<p id="assign">Assigning data attribute</p>
jquery
$("#assign").data("title","firstparagraph");

Output:
<p id="assign" data-title="firstparagraph">Assigning data attribute</p>

For getting attribute data from the element
Html
<p id="assign" data-title="firstparagraph">Assigning data attribute</p>
jquery
console.log($("#assign").data("title"));
console.log($("#assign").data());

Output:
firstparagraph
{ title: firstparagraph }

Example 2: simple return data jquery

<?php

include "config.php";

$return_arr = array();

$query = "SELECT * FROM users ORDER BY NAME";

$result = mysqli_query($con,$query);

while($row = mysqli_fetch_array($result)){
    $id = $row['id'];
    $username = $row['username'];
    $name = $row['name'];
    $email = $row['email'];

    $return_arr[] = array("id" => $id,
                    "username" => $username,
                    "name" => $name,
                    "email" => $email);
}

// Encoding array in JSON format
echo json_encode($return_arr);

Tags:

Java Example