Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I am New to JS

In HTML I have 2 pages

1st page contain 1 textbox
2nd page contain 1 label

I want to show textbox value in label Using JS But Realtym . If i enter any value in textbox it reflect same tym on label

What I have tried:

I failed to do that Is it possible . i search everywhere i didnt find
Posted
Updated 31-Oct-17 20:21pm
Comments
Karthik_Mahalingam 1-Nov-17 2:00am    
2nd page is a pop up ?
Gurpreet Arora Malhotra 1-Nov-17 2:01am    
No pop up 2nd page is simple Html Page

1 solution

Page 1
<html>
<head>
    <script>
        function keypress() {
            var txt1 = document.getElementById("txt1");
            var value = txt1.value;
            setCookie('txt1value', value, 1)
        }
        function setCookie(cname, cvalue, exdays) {
            var d = new Date();
            d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
            var expires = "expires=" + d.toUTCString();
            document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
        }

    </script>
</head>
<body>
    <input id="txt1" type="text" onkeypress="keypress()" onkeyup="keypress()"><br>     
</body> 
</html>

Page 2
<html>
<head>
    <title>Page 2</title>
    
</head>
<body>
   <span id="lbl"></span>

    <script>
        setInterval(function () {
            var value = getCookie('txt1value')
            document.getElementById('lbl').textContent = value;
        }, 100)



        function getCookie(cname) {
            var name = cname + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') {
                    c = c.substring(1);
                }
                if (c.indexOf(name) == 0) {
                    return c.substring(name.length, c.length);
                }
            }
            return "";
        }
    </script>
</body>
</html>

Note: you will have to host the pages to make it work
Refer these
JavaScript Cookies[^]
Window setInterval() Method[^]
 
Share this answer
 
Comments
Gurpreet Arora Malhotra 1-Nov-17 2:34am    
Not working
Karthik_Mahalingam 1-Nov-17 2:45am    
haved tested it, working for me.
how did you run the page.
Gurpreet Arora Malhotra 1-Nov-17 2:47am    
why we use cookies there. any other way
Karthik_Mahalingam 1-Nov-17 2:59am    
then how will you get the data in real time ?
to pass the data we can use query string, but you need in real time
Gurpreet Arora Malhotra 1-Nov-17 4:07am    
Can we do it with ajax

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900