1

i have a script that loads some div ids from php and every x seconds reloads the new values.. I want to implement an onchange value inside the script in order to trigger loading a new variable if (in this example artist changes) but i cant figure out how.. the script is:

setInterval(function(){
    cache: false,
    $("#artist").load("test.php #artist");
    $("#song").load("test.php #song");
}, 2000);

I need to tell if artist changes then load a new variable from php, using this wrapper code i found here How can I make a program wait for a variable change in javascript??

function Wrapper(callback) {
    var value;
    this.set = function(v) {
        value = v;
        callback(this);
    }
    this.get = function() {
        return value;
    }  
} 

If i use the same example with input box:<input type="text" onchange="wrapper.set(this.value)"/> it works.. But i cant figure out how to make it work using the #artist pulled from the php in the first part..

I tried doing this :

<script type="text/javascript">
    setInterval(function(){
        cache: false,
        $("#artist").load("test.php #artist").onchange(wrapper.set(this.value));
        $("#song").load("test.php #song");
    }, 2000);
</script>

and a lot of other combinations but neither works.. Can you help me please!

PS: Keep in mind that im a startet in javascript..

Thank you for your answers.

Community
  • 1
  • 1
Sam F
  • 13
  • 6
  • You are missing something here for a start `$("")`. I'd advise you to use firebug in firefox to debug the code – Zevi Sternlicht Nov 10 '13 at 13:10
  • @InGodITrust sorry mate small typo havent noticed my bad.. i removed it. – Sam F Nov 10 '13 at 13:18
  • you still have one more of those ^^ – Jaak Kütt Nov 10 '13 at 13:21
  • Try this i haven't tested this script so it may work – Waheed Rahman Nov 10 '13 at 13:58

1 Answers1

0
setInterval(function(){
    cache: false,
    $.get('test.php',function(data){
     var artist = $('<div>' + data + '</div>').find('div#artist').html();
     if(artist != $('#artist').html()){
          $('#artist').html(artist );
          wrapper.set(artist)
    } //in here I assumed #artist is a div or span,... if its a textbox change it to $('#artist').val()
    });
    $("#song").load("test.php #song");
}, 2000);
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • there was no html code in your question.if #artist is a textbox just tell me to edit the answer. – Ashkan Mobayen Khiabani Nov 10 '13 at 16:13
  • This seems to do the trick, thank you very much, but it gives me every 2sec all the html data.. My syntax regarding artist in php file is "echo "
    $artist
    ";" How can i make it to work only for artist and only if the name changes ?
    – Sam F Nov 10 '13 at 16:25
  • It works perfect now.. Thank you.. If i wanted to take the $artist variable from the test.php with this script how could this be done, rather than getting the div? Im just curious :) – Sam F Nov 10 '13 at 16:44
  • please vote up too ;) well in your php file you can just echo the artist name without anything else no html tags no script. – Ashkan Mobayen Khiabani Nov 10 '13 at 16:46
  • if you had just read the name not the div then variable data would contain the artist name no extra effort would be needed to extract it. – Ashkan Mobayen Khiabani Nov 10 '13 at 16:51
  • Now i get it thanks for pointing this. I wish i could vote u up but i need more than 15per else i would definitely vote u up :) Thanks once more for helpin – Sam F Nov 10 '13 at 17:04