Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
I have this code:
HTML
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
<pre lang="Javascript">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
        </script>
<script type="text/javascript">
$("#readfileline").click(function(){
                 alert("I am here !");
            file = "http://localhost/_AjaxPhpArr/uplods/test.txt"
            function getFile(){
               
                        $.get(file,function(txt){
                            var lines = txt.responseText.split("\n");
                            for (var i = 0, len = lines.length; i < len; i++) {
                                save(lines[i]);
                                alert(""+lines[i]);
                                alert("albab");
                            }
                        }); 
                    }
            });
        </script>
</head>
    <body>
        <?php
        // put your code here
        ?>
        
        <input type="submit" id="readfileline" value="submit"/>

    </body>
</html>


Now when I am clicking in the button this is not calling Jquery function. how can I solve this?
Posted
Comments
Richard C Bishop 8-Apr-13 15:29pm    
You are failing to call the $(document).ready(function(){});

It is required when using jQuery.
Sandeep Mewara 8-Apr-13 16:03pm    
You write function inside function, looks incorrect syntax. Please check.
lewax00 8-Apr-13 18:08pm    
In JS you can declare functions inside of functions, it is perfectly valid.
Sandeep Mewara 9-Apr-13 1:59am    
I know it can be. I meant, the way it has been done. Will have to try this kind of syntax to be sure.
Moykn 11-Apr-13 8:57am    
Actually $(document).ready is not required, it will only guarantee that the code inside function will run after the DOM is loaded.
If you put all the scripts in the bottom of the page it will works.
In this specific case the click function is not working because he tries to find the "readfileline" buttom before it exists, so he could just move his script to the bottom of the page or wrap it inside a $(document).ready call.

Of course not because you have reintroduced the bug that I solved for you in this[^] question earlier today.

JavaScript
$(document).ready(function() { $('#readfile').click(function(){ alert('hello'); }); });
 
Share this answer
 
Hello,

Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol. In short you will not be able to do this via get method if the text file is being served from other domain. Otherwise you can alter your get method to include a 'text' as the last parameter. Here is the modified script.
JavaScript
function readFile() {
    $.get("http://wordpress.org/extend/plugins/about/readme.txt", 
        function(txt, status, jqXHR ) {
            var lines = txt.split("\n");
            for (var i = 0, len = lines.length; i < len; i++) {
                save(lines[i]);
            }
        },
        "text"
     );
}

Regards,
 
Share this answer
 
v4
Comments
UL UL ALBAB 9-Apr-13 14:02pm    
No this code is not working.
Prasad Khandekar 9-Apr-13 16:05pm    
I have updated the solution. It works now.

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