When a string inside a div changes do something

JavaScript and client side scripting.

Moderator: General Moderators

klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

When a string inside a div changes do something

Post by klevis miho »

I want that if a string inside a div changes, an ajax box will be changed
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: When a string inside a div changes do something

Post by kaszu »

More info please? html, js?
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: When a string inside a div changes do something

Post 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.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: When a string inside a div changes do something

Post by kaszu »

You should post some html/js and what you already tried, what were the problems. I'm assuming you tried something.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: When a string inside a div changes do something

Post by Eran »

The event that changes the song title should also change the information about the artist
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: When a string inside a div changes do something

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: When a string inside a div changes do something

Post 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() )
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: When a string inside a div changes do something

Post 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)?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: When a string inside a div changes do something

Post 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)
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: When a string inside a div changes do something

Post by klevis miho »

Very thanks man, will try that out and tell you.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: When a string inside a div changes do something

Post 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]
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: When a string inside a div changes do something

Post by Eran »

Code: Select all

var div = document.getElementById('aaa');
alert(div.innerHTML);
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: When a string inside a div changes do something

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: When a string inside a div changes do something

Post by Eran »

What is the problem? use setInterval to keep checking it until the contents changes. It doesn't matter if it starts empty...
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: When a string inside a div changes do something

Post 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?
Post Reply