I am trying to fix a problem for hours and I just gave up trying to search & code & see if it works.
Maybe it will be better to explain with simple code what I am trying to do;
<script>
test1 = $('input:text[name=test1]');
test2 = $('input:text[name=test2]');
test3 = $('input:text[name=test3]');
var obj = [];
obj.push(test1);
obj.push(test2);
obj.push(test3);
$.each(obj, function(key, value) {
console.log ('value : ' + value.val());
});
</script>
</head>
<body>
<input name="test1" type="text" value="1"/>
<input name="test2" type="text" value="2"/>
<input name="test3" type="text" value="3"/>
</body>
</html>
What I am trying to do is;
- Define variables which will be assign to DOM element.
- Create an array and put those variables which are assigned to DOM elements.
- Loop them and get their values etc...
Is there a way to do that? I am really tired after trying to make it work for hours.
Thank you for your help and time in advance.
EDIT:
Of course the code above wasn't something I was trying to achieve. I have a form which has over 700 lines of Jquery code and I had a problem in one of my functions.
The principle was the same so I decided to make something really short and clearly understandable. $value was just bad copy & paste. Before I realized I didn't delete all of my old code (just $ was left) and edited my post, some people already replied. It wasn't the correct answer but I just didn't want to close the question without giving points to anyone.
The code above will not work unless if you change it to;
$(document).ready(function(){
// Put the code here
});
Considering my jquery code is in between <head> </head> I have to use $(document).ready
I hope this will help someone.
I am not newbie in Jquery, I don't know why and how I could make such mistake.