Simple answer:
The jQuery library used by my code base is deprecated.
If you are out of date, try the following:
A step, despite an unknown version of jQuery, to find out if the problem is inside the library (which is 9 times out of 10 probably won't)
When all else fails, just write a clean javascript solution.
Sometimes, when I write a “class” in javscript with jQuery, jQuery simply will not function properly. For example, today I did the following when selecting input:
$(this).val(newValue);
This worked in jFiddle just fine, but did not work in my project (both had 0 script errors). I tried to isolate the code as much as possible, but to no avail I could not get it to work.
The “solution” for this was to simply write straight up javascript to set the value, and it worked fine.
I am not a jQuery master, but this is not the first time I have had to write a direct javascript solution when jQuery "failed". Do any jQuery wizards know why something like this could happen? Is there a jQuery debugging method that I don't know about? Does anyone else encounter these issues in jQuery? If so, do you have a solution?
Update (s):
August 25, 2011
I downloaded the latest version of jQuery library and went through it only to find that the error was probably resolved between jQuery 1.4.4 and 1.6.2. I did not understand that the person who processes most of javascript is not updating the jQuery library. After navigating through the jQuery 1.4.4 library, it seems like for some reason jQuery could not find my selector and therefore never set the value. This problem was resolved in 1.6.2 ....
Lesson of the week ... update your jQuery library and check your senior developer statements when things don't match.
-
I chose the answer I made because it was the only one that really helped me diagnose the source of the problem. Although upgrading to an unminified version of jQuery is a simple solution, I had to do this before posting this question. I will post my findings on why this did not work later.
August 24, 2011
I agree with Sohnee user that having a duplicate "name" is not a bad practice, and in some places you really need it.
I feel pretty stupid because the published code was wrong for almost a week. I updated the code. I moved the init function to the public domain.
August 22, 2011
Although I'm partially satisfied that the core of the problem is with duplicate names, I need to know why duplicate names are bad?
I know that when dealing with the inputs / css / etc, you usually exclude identifiers to be unique, and classes to represent groups. I did not think that there were any rules regarding names , but if someone can explain to me why having duplicate names is bad practice, I will consider the answer to this problem.
August 16, 2011
Regarding the current answers, I don't think this is a conflict. jQuery works fine. Couplings start by calling functions called in both implementations.
Problem line
$(overrideSelector).each( function(){ $(this).val(newVal); } );
For example, if I console newVal in .each (), it will have the value let say 'A'.
Then if I remove the console $(this).val() , it will be "B". Then $(this).val(newVal); . After that, if I do the console log $(this).val() , it will still be "B".
In the comments, someone said that I may use the word this incorrectly. Both of them returned 0 javascript errors on the Chrome console. I will give the following code snippet, which is why problems arose. I will write the original, and then that I replaced it with javascript.
I know that this is one and the same, but that’s normal. The page I'm working on is a huge form, and the cloned selection is just to make the user not go back to another part of the form.
HTML:
<div id='overrideHolder'> </div> <select name='mySelect'> <option value='A'>A</option> <option value='B'>B</option> </select>
jQuery ( not working):
var someClass = (function(){ var overrideSelector = '[name="mySelect"]'; ... return{ init : function(){ $(overrideSelector).clone().appendTo('#overrideHolder'); $(overrideSelector).each( function(){ $(this).bind( 'change', {}, function(){ someClass.overrideTryAgain(this); } ); } ); } overrideTryAgain : function(element){ var newVal = $(element).val(); $(overrideSelector).each( function(){ $(this).val(newVal); } ); ... }, ... } })(); $(document).ready(function(){ someClass.init(); }
Javascript (this works):
var someClass : (function(){ var overrideSelector = '[name="mySelect"]'; ... return{ init : function(){ $(overrideSelector).clone().appendTo('#overrideHolder'); $(overrideSelector).each( function(){ $(this).bind( 'change', {}, function(){ someClass.overrideTryAgain(this); } ); } ); } overrideTryAgain : function(element){