1

I have this scenario: I have a simple php file with only few html elemnts: a div called switch, another called lamp and a couple of buttons. The two buttons are labeled On and Off. The lamp div is empty. The switch div is empty too, but is updated using jQuery and Ajax with the content of a txt file, that only contains one word: it could be On or Off. What i'm traying to achieve is this: whenever the file is updated with the word On or Off i would like the On or Off button to be triggered correspondingly and the lamp div to change the background color. Is it possible? UPDATE:

Example:

(function($){
  $(document).ready(function() {
  $.ajax({
    url : "testfile.txt",
    dataType: "text",
    success : function (data) {
       $("#switch").html(data);

       // this doesn't seems to work...
       var word = data.toLowerCase();
       $('#' + word).trigger('click');

        // this works 
        $(document).ajaxStop(function(e){
            var response = $("#switch").html();
            $("#" + response.toLowerCase()).trigger("click");
        });

        var $container = $("#switch");
        var refreshId = setInterval(function()
        {
            $container.load('testfile.txt').html();
        }, 2000);
      }
  });
}); 
})(jQuery);

<div id="switch"></div>
<div id="on" class="button">On</div>
<div id="off" class="button">Off</div>

<div id="lamp"></div>
1
  • When you say trigger, do you mean click, or a certain set of code is triggered? Commented May 18, 2015 at 9:20

3 Answers 3

1

Since the response is only one word. Why not try

var word = data.toLowerCase();
$('#' + word).trigger('click');

in the success callback.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this only works if i refresh the page (after changed the content of the file), it doesn't update dinamically
@uomopalese, did you put this code in the ajax success callback? What do you mean dynamically? When the page first loads?
@AmmaCSE I updated the code to show you were I put it. I don't know why one works and the other doesn't.
0

If you have only one ajax request, you can do like this:

$(document).ajaxStop(function(e){
  var response = $("#switch").text();
  // do what you want with variable response here
  $("#" + response.toLowerCase()).trigger("click");
});

Comments

0

Maybe this can help for what you need:

(function($){
    $(document).ready(function() {
        var $container = $("#switch");
        $container.load("testfile.txt", function() {
            setInterval(function() {
                $container.load("testfile.txt");
            }, 2000);
        });
    }); 
})(jQuery);

Use clearInterval() to stop the timer when needed.

2 Comments

Where I have to put clearIntervall() ? The browser is sending cuntinuous requests to the server. Thanks
Your setInterval makes a request every 2s and updates the container as long as the page is loaded. This is how you can detect changes in the file. You can use clearInterval on some user action or when a certain condition is met.

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.