corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » Javascript/jquery gurus?


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author Javascript/jquery gurus?
Rob_Quads
Member

Registered: 29th Mar 01
Location: southampton
User status: Offline
26th Jul 12 at 13:17   View User's Profile U2U Member Reply With Quote

I'm playing around with a home central heating system and hitting a stumbling block on jquery screwing up some of my javascript.

My page has a form which calls a function onchange="override_temp_changed('hot_water')". This function is declared in a .js file and has the following inside...

function override_temp_changed(control_group) {
$("control_group_changed").value = control_group;
$("override_type_changed").value = "temp";
console.debug("Temp change made to " + $("control_group_changed").value + " type: " + $("override_type_changed").value );
$("formoverride").submit();
}

This all works fine. When you select the drop down it logs to the javascript console such as..
--
[14:13:29.083] Temp change made to hot_water type: temp @ xxxxx/home.js:21

BUT as soon as I have jquery.js imported as a script i.e. <script src='../../jquery.js'></script> the commands inside the function are not working. Its going into the function as there is output the syslog such as

--
[14:15:37.836] Temp change made to undefined type: undefined @ xxxxx/home.js:21

Any ideas?? Annoying as once I have jquery in there all my websocket code is working a treat giving me live updates without any reloads.



ed
Member

Registered: 10th Sep 03
User status: Offline
26th Jul 12 at 15:51   View User's Profile U2U Member Reply With Quote

You've not used the correct syntax for the selector. jQuery uses Sizzle so you need to use CSS style syntax:

ID's should be #this-is-an-is
Classes should be .this-is-a-class
Name atributes should be input[name=my-input-name]
Tags should be thins like input or div or header
ed
Member

Registered: 10th Sep 03
User status: Offline
26th Jul 12 at 15:53   View User's Profile U2U Member Reply With Quote

Also that's not how you set something's value in jQuery. You use something like this:

$('input[name=my-input]').val('This is the new value');

Also, to write to the console you use console.log - I'm not sure if console.debug does anything - ignore me if it does.

[Edited on 26-07-2012 by ed]
Rob_Quads
Member

Registered: 29th Mar 01
Location: southampton
User status: Offline
26th Jul 12 at 16:36   View User's Profile U2U Member Reply With Quote

Hmm on further research its to do with the way jquery handles $. In query this means something different to normal javascript so while the original code was fine as soon as you import jquery its then interpreted differently.

The working code is

function override_temp_changed(control_group) {
document.getElementById("control_group_changed").value = control_group;
document.getElementById("override_type_changed").value = "temp";
document.getElementById("formoverride").submit();
}

Thanks for the help....its slowly getting there. Now to get the wireless relays working
ed
Member

Registered: 10th Sep 03
User status: Offline
26th Jul 12 at 16:45   View User's Profile U2U Member Reply With Quote

Huh? $ is the name of the jQuery object, it doesn't do things using standard JavaScript methods and objects because it's not meant to. It's a library with its own syntax that must be used.
Rob_Quads
Member

Registered: 29th Mar 01
Location: southampton
User status: Offline
26th Jul 12 at 17:02   View User's Profile U2U Member Reply With Quote

It must have been doing something as with the old code it was working.
Dom
Member

Registered: 13th Sep 03
User status: Offline
26th Jul 12 at 18:43   View User's Profile U2U Member Reply With Quote

As mentioned, the undefined error is due to your malformed JQ ie: no proper selectors, wrong use of setting object/element values.
The dollar sign is purely an alias of 'jQuery.'. However you can have conflicts when using multiple libraries (Prototype etc) using the dollar sign hence why you should use 'noconflict' function.

As said though, rewrite the JQ to conform to the syntax and you should be ok.

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
flash script help Matt L Geek Day 14 581
11th Jan 09 at 01:40
by Ian
 
CSS help Butler Geek Day 3 654
1st Aug 09 at 14:00
by liamC
 
Any Javascript peeps on here? ed Geek Day 7 470
10th Mar 10 at 18:40
by ed
 
AJAX people.... AndyKent Geek Day 16 277
18th Jul 10 at 15:38
by Dom
 
Need a form making... Half way there... need some Java guys in here! Gary Geek Day 32 1786
4th Nov 10 at 13:32
by ed
 

Corsa Sport » Message Board » Off Day » Geek Day » Javascript/jquery gurus? 28 database queries in 0.0110221 seconds