Page 1 of 2
When a string inside a div changes do something
Posted: Sat Apr 17, 2010 8:56 am
by klevis miho
I want that if a string inside a div changes, an ajax box will be changed
Re: When a string inside a div changes do something
Posted: Sat Apr 17, 2010 10:04 am
by kaszu
More info please? html, js?
Re: When a string inside a div changes do something
Posted: Sat Apr 17, 2010 10:10 am
by klevis miho
Ok. The situation is this:
I have the title of a song displayed inside a div, and it changes every minute.
I want to display information about the singer in another div, without reloading the whole page.
Re: When a string inside a div changes do something
Posted: Sat Apr 17, 2010 1:54 pm
by kaszu
You should post some html/js and what you already tried, what were the problems. I'm assuming you tried something.
Re: When a string inside a div changes do something
Posted: Sat Apr 17, 2010 1:57 pm
by Eran
The event that changes the song title should also change the information about the artist
Re: When a string inside a div changes do something
Posted: Sat Apr 17, 2010 2:03 pm
by klevis miho
I am not so familiar with javascript. I tried this:
<div id="song_title" onChange="alert('test');"> </div>
This div is filled from an external js.
Pytrin, it hasnt any other information about the artist. I'll put the information manually into my db. Then getting the band nae from the song title, I will fetch the information from the db and showing it on a div, without reloading the page.
Re: When a string inside a div changes do something
Posted: Sat Apr 17, 2010 2:06 pm
by Eran
You should check if that external script has any bindings that would allow you to hook into the song change event.
The onchange event is only supported by form inputs (including select and textareas), so you could using that with an input instead of a div. Your last resort should be using a timer to check if the contents have changed (using setInterval() )
Re: When a string inside a div changes do something
Posted: Sat Apr 17, 2010 2:14 pm
by klevis miho
Very thanks pytrin.
So I will get this element by id f.e. every 5 secons using setInterval() right?
But how can I get two values from the same div and compare them(to see if the content has changed)?
Re: When a string inside a div changes do something
Posted: Sat Apr 17, 2010 2:17 pm
by Eran
You should store the content in a variable outside the setInterval callback, and compare to it on each iteration. Update that variable when the content changes (and also run your intended code of course)
Re: When a string inside a div changes do something
Posted: Sat Apr 17, 2010 2:19 pm
by klevis miho
Very thanks man, will try that out and tell you.
Re: When a string inside a div changes do something
Posted: Sat Apr 17, 2010 3:52 pm
by klevis miho
How can I get the content inside a div?
I use this:
<div id="aaa"> test test test </div>
<script type="text/javascript">
var test = document.getElementById('aaa');
document.write(test);
</script>
and it outputs me: [object HTMLDivElement]
Re: When a string inside a div changes do something
Posted: Sat Apr 17, 2010 4:07 pm
by Eran
Code: Select all
var div = document.getElementById('aaa');
alert(div.innerHTML);
Re: When a string inside a div changes do something
Posted: Sat Apr 17, 2010 4:26 pm
by klevis miho
Thnx again man.
But I have another problem.
This div is empty, it gets filled from an external js.
I think I'll read a js book to get it.
http://radiospat.com is the project I am working with this js and div's
Re: When a string inside a div changes do something
Posted: Sat Apr 17, 2010 4:34 pm
by Eran
What is the problem? use setInterval to keep checking it until the contents changes. It doesn't matter if it starts empty...
Re: When a string inside a div changes do something
Posted: Sat Apr 17, 2010 5:00 pm
by klevis miho
I don't know how to do it:
function check() {
//
}
setInterval("check()", 10000);
var title = document.getElementById('cc_stream_info_song');
title = title.innerHTML;
Im stuck.
Doesnt it exist something like onChange for other things than forms?