1

I have a text file which has one value in it, this value is updated over time but that doesn't matter, the thing is I want to get this value from the .txt file using javascript and then when I have gotten this value, I would like to change my current variable value to that new value from the .txt file. Because there is only one value at a time like 1 or 10, it just needs to get the value in the .txt file and not a specific value.

My javascript/html so far:

<div class="curVariable">
Cur variable: <span id="curVar"></span>
</div>

<script type="text/javascript"> 

var curVar= 1;
document.getElementById("curVar").innerHTML = curVar;

</script>
1
  • If the text file is on client side, and you want to read locally, then you should use ajax.. Commented Apr 4, 2015 at 17:04

1 Answer 1

1

Assuming that the file is on the server, use the following jQuery code:

   $(document).ready(function() {
    $("#butt").click(function() {
        $.ajax({
            url : "helloworld.txt",
            dataType: "text",
            success : function (data) {
                $(".text").html(data);
            }
        });
    });
}); 
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think it's appropriate to click other people's #butts in the work place :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.