Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help setting field value from checkbox value using jQuery

 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So with my code I'm looking to take a value out of a checkbox and put it into a text field. The code is in JSFIDDLE. See the link below. I use the latest JQuery as well.

http://jsfiddle.net/ddemott/MUU3z/

The problem I'm having is I can't seem to get the checkbox value go into the input field. It pops up in the alert but won't go into the field. I'm obviously missing something. Here's the code snippet.



Here's the code. As you can see I look to see how many checkboxes were checked. If there was only ONE checkbox checked then I popup up the value in an alert. But when I try to put x into the field of 'returnValue' it doesn't change the input field. If you go to jsfiddle you'll see this code execute when you click a single room.

Below is all of the code (without the script tags)



and the HTML

 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is setting the value correctly, you just have too much logic going in multiple places that resets the value. [also use console.log() to debug and not alerts]

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Eric -- the code is not logically organized and is stepping all over itself. I'd actually advise deleting the whole thing, taking a deep breath, figure out what you're actually trying to accomplish, and try again.
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once I cleaned it up and got my head around the logic it fell together much nicer. All is working now.

See code in JSFiddle.

http://jsfiddle.net/ddemott/5k396/38/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic