Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

2018/09/20

PHP - How to pass variables to Javascript

This post is about how to pass PHP variable to Javascript.

Sample Code (example.php)

<?php
$user = "Bernie";
?>
<div class = "container" style = "background-color:#332B57;">
    <div class = "content">
    </div>
</div>
<div class = "container">
    <div class = "content">
    </div>
</div>
<script>
function welcomeMsg(){
    var user = "<?php echo $user; ?>";
    alert(user);
}
window.addEventListener('load',welcomeMsg);
</script>

2018/02/07

How to get the selected value from within a table using JavaScript.

This post is about how to get the selected value from within a table using JavaScript.

The Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <table id="dataTable">
            <tr>
            <td><input value="first" name="first" /></td>
            <td><input value="second" name="second" /></td>
            <td>
            <select name="third">
                <option value="option1">One</option>
                <option value="option2">Two</option>
            </select>
            </td>
            </tr>
        </table>

        <button onclick="getData()">Click</button>
    
        <script>
            function getData(){
                var table = document.getElementById("dataTable");
                var row;
                for (var i = 0,  row = table.rows[i]; i < table.rows.length ;i++) {
                    console.log('here')
                    var x = row.cells[0].childNodes[0].value;
                    var y = row.cells[1].childNodes[0].value;
                    var z = row.cells[2].childNodes[1].value; // select option field
                    console.log(x,y,z);
                }
            }
        </script>     
    </body>
</html> 

The Result


The HTML Structure


Reference:

How to get selected option value of all rows of table in JavaScript?
https://stackoverflow.com/questions/43094914/how-to-get-selected-option-value-of-all-rows-of-table-in-javascript

2018/02/05

How to get the ID of the clicked button using JavaScript

This post is about how to get the ID of the clicked button using JavaScript.

The Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
    <body>

        <button id="1" onClick="reply_click(this.id)">B1</button>
        <button id="2" onClick="reply_click(this.id)">B2</button>
        <button id="3" onClick="reply_click(this.id)">B3</button>

    <script type="text/javascript">
        function reply_click(clicked_id)
        {
            alert(clicked_id);
        }
    </script>
    </body>
</html>

The Result

Reference:

JavaScript - onClick to get the ID of the clicked button
https://stackoverflow.com/questions/4825295/javascript-onclick-to-get-the-id-of-the-clicked-button

How to add event listener to a dynamically created button using JavaScript.

This post is about how to add event listener to a dynamically created button using JavaScript.

The Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html>
<html>
    <head>
    </head>
    
    <body>
        <form>
            <div class="control">
                <label>Name</label>
                <input id="txtName" name="txtName" type="text" />
            </div>
            <div class="control">
                <label>Mobile</label>
                <input id="txtMobile" type="text" />
            </div>
            <div class="control">
                <input id="btnSubmit" type="button" value="submit" />
            </div>
        </form>
        
        <script>
            document.addEventListener('DOMContentLoaded', function() {
                document.getElementById('btnSubmit').addEventListener('click', function() {
                    var name = document.getElementById('txtName').value;
                    var mobile = document.getElementById('txtMobile').value;
                    var html = '<ul>';
                    for (i = 0; i < 5; i++) {
                        html = html + '<li>' + name + i + '</li>';
                    }
                    html = html + '</ul>';
                
                    html = html + '<input type="button" value="prepend" id="btnPrepend" />';
                    document.getElementsByTagName('form')[0].insertAdjacentHTML('afterend', html);
                
                    document.getElementById('btnPrepend').addEventListener('click', function() {
                        var html = '<li>Prepending data</li>';
                        document.getElementsByTagName('ul')[0].insertAdjacentHTML('afterbegin', html);
                    });
                });
            });
        </script>  
    </body>
</html>

The Result









2017/03/27

How to sort multi-dimensional arrays in JavaScript

This is a continuation of my previous post on How to create a line chart using Chart.js and the data parsed from a CSV string using Papa Parse. In the previous post, the x-axis is not in ascending order (see below photo). This post will address this issue by sorting the data in the desired order before making the chart.


2016/12/17

2016/12/06

How to get input from check boxes and toggle switches

This post is about how to get the input value from check boxes and toggle switches.

Getting the input from check boxes

2016/11/11

2016/11/10

Notes for JavaScript Tutorial for Beginners - Section 1 to 35

Here I put my notes for section 1 to 35 of the "JavaScript Tutorial for Beginners" on Youtube.

Typical HTML Template

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<!DOCTYPE html>

<html>
 
<head> 
<script>


</script>
</head>

<body>

</body>

</html>

2016/11/04

Google Drive API - Listing all files on a client's Google Drive using JavaScipt

The code below runs on a web sever and it lists all files on Google Drive. Be sure to put in your client id (highlighted in yellow below) before running it.