tags:

views:

143

answers:

5

Hi, I'm new to JQuery. I'm having a

<div id='field1'>  </div>

I'm referring it twice in my code ..But at each time it refers to the same 1st reference.

Eg .. I'm changing the DIV content initially with the value

$("#field1").change(function (){alert('hi')});

After some piece of code inside one another click function I'm having

$("#field1").change(function(){alert('again')});

When i want to make use of the second change function with some other content ,the first change function is called How can i dtop the first change not to be called..and only the second one to be called...

But this second one refers to the first one. How can i rectify this? I've even used $(this) in my second change. http://stackoverflow.com/questions/950037/same-ajax-called-twice-jquery Please suggest me..

+2  A: 

If you mean you have two DIVs with the same ID, that's actually invalid HTML. IDs should be unique per-document.

Rytmis
NO only one div i m using them twice with different content
Aruna
Do you have id="field1" twice in your application? If yes, it is invalid. It doesn't matter that the content is different.
kgiannakakis
please explain how you do that, using them twice with different content.
Natrium
NO . i m having a single DIV ...Art first i used a show of tat DIVwith $("field1").change(){});after sometime with the other content i m updating the div again
Aruna
http://stackoverflow.com/questions/950037/same-ajax-called-twice-jqueryThis is my actual code is.
Aruna
A: 

the # selector selects an element with the ID #first1 in the html document which would be the first div since ID's are supposed to be unique.

you could use a class and the . selector to select both div's or alternatively give the second div a different name Information about selectors in jQuery

Stephen Binns
A: 

You miss a dot:

 $("#field1").change(function (){
    alert('hi');
 });

And you can't use the same name twice.

MrHus
A: 

I think jQuery when using ID's, only ever returns one element.

If you must use the same selector name (which as Rytmis correctly stated, using multiple ID's with the same name is invalid HTML), then changing to a class selector would be best.

You can then use the :eq() functionality in jQuery

To target your first div you could use:

jQuery('.field:eq(0)')

and then to target your second div use:

jQuery('.field:eq(1)')

Remember they come back as an array so 0 = first, 1 = second, etc...

danrichardson
$(".TextFieldSettings #fieldTitle").change();//First$(".TextFieldSettings #fieldTitle").change();//secondHow can i use :eq(0) for first and eq(1) for second .Please suggest mewith an example..
Aruna
I think you misread the question. Although I must say I had to read it three times before I figured it out (more or less)
Philippe Leybaert
you cannot. jQuery will find all elements with the .TextFieldSettings (in this case 2 elements), but then will always only pull back the first element with the #fieldTitle ID.Using ID's as a selector in jQuery will always come back with only one element.A simple test would show 1 when the following is run (assuming firebug/firefox is used) - console.log(jQuery("#fieldTitle").length);
danrichardson
+7  A: 

What you are attempting to do is not clear from the way you asked the question. From what you put in code I would guess that you are trying to modify the onChange event of the div. So to start with you have:

$("#field1").change(function (){alert('hi')});

And when #field1 changes you want to fire a function that calls alert('hi')

Later you have button that runs:

$("#field1").change(function(){alert('again')});

And you want changes to #field1 to fire a function that calls alert('again')

But what you are getting when the change is happening is an alert box with 'hi' in it.

What is happening is that both alert('hi') and alert('again') are getting bound to the change event. If you want you to replace the change event, you need to first use the unbind( type, fn ) function.

If you want them both to fire, you need to 'return false;' from your change functions.

lambacck
+1 for deducing the actual problem. :)
Rytmis
+1, very thorough.
Ben Blank