Solved

jQuery sort losing if radio is checked or not.

Posted on 2009-07-15
7
555 Views
Last Modified: 2013-11-19
Wilq32 helped me with this in the past.

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_24422937.html

He help me to get my inputs to retain their value when they add another item to the sort list. Now I need to do the same thing for a radio, retain if it is checked or not.

wilq32 said: "

Before $select.empty()  do:


$("#asmList0 li").each(function()
{
var value=($("input",this).attr("value"));

$("#"+this.getAttribute('rel')).attr("last",value);
});

then in addListItem do that:

change this:

 var $itemInput = $('<input type="text"/>').addClass(options.inputItemLabelClass);

to

var $itemInput = $('<input type="text"/>').addClass(options.inputItemLabelClass);
$itemInput.attr("value",$O.attr("last"));
"

As I said that works for my input item. I changed it to not select an input and instead set an id that it targets. So now I tried to get it to work for keeping the radio checked.

My code below does not work for the radio but still works for the value for the text input:
var value=($('#pageNameInput',this).attr('value'));

var radioChecked=($('.homeRadio').attr('checked'));//is the radio checked or not?
 

$("#"+this.getAttribute('rel')).attr("last",value);

$("#"+this.getAttribute('rel')).attr("radio",radioChecked);
 

and
 

var $homeRadio = $('<input type="radio"/>').addClass(options.homeRadio);

	$homeRadio.attr("id", "home_Radio");

	$homeRadio.attr("name", "home-radio");

	$homeRadio.attr("checked",$O.attr("radio"));

Open in new window

0
Comment
Question by:the_hero
  • 4
  • 2
7 Comments
 
LVL 1

Author Comment

by:the_hero
Comment Utility
This kinda works. It checks the last added one instead of if the element was checked before
var value=($('#pageNameInput',this).attr('value'));

var radioChecked=($('.homeRadio', this).attr('checked'));//is the radio checked or not?
 

$("#"+this.getAttribute('rel')).attr("last",value);

$("#"+this.getAttribute('rel')).attr("checkRadio",radioChecked);
 

and
 

var $homeRadio = $('<input type="radio"/>').addClass(options.homeRadio);

	$homeRadio.attr("id", "home_Radio");

	$homeRadio.attr("name", "home-radio");

	$homeRadio.attr("checked",$O.attr("checkRadio"));

Open in new window

0
 
LVL 1

Author Comment

by:the_hero
Comment Utility
Here is my test page:

http://dalelarsen.com/dale/sort.php
0
 
LVL 18

Expert Comment

by:wilq32
Comment Utility
Your change is basically ok, one thing to change:

   $homeRadio.attr("checked",$O.attr("checkRadio"));


replace this to:

if ($O.attr("checkRadio"))  $homeRadio.attr("checked",true);




Its because  there is nothing like checked=false, its checked or nothing ;)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:the_hero
Comment Utility
Thanks for helping with this wilq32.

It still does not work though. Seems about the same.

I tried a few new things out but they didn't work.

I updated my example page,

http://dalelarsen.com/dale/sort.php
0
 
LVL 18

Accepted Solution

by:
wilq32 earned 500 total points
Comment Utility
I dont see mine changes on test case :(

Anyway I saw in debugger that function  .attr("checkRadio") returns string instead of value.... Try this. Maybe it helps




if ($O.attr("checkRadio")==="true")  $homeRadio.attr("checked",true);
0
 
LVL 1

Author Closing Comment

by:the_hero
Comment Utility
Perfect thanks! I will be doing several other functions like this, like making it remember which <option> is selected for each select in the sort menu and such. If I need help I'll post more.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now