Solved

jQuery sort losing if radio is checked or not.

Posted on 2009-07-15
7
560 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:Shaye Larsen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 1

Author Comment

by:Shaye Larsen
ID: 24864884
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:Shaye Larsen
ID: 24865157
Here is my test page:

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

Expert Comment

by:Pawel Witkowski
ID: 24893002
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
Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

 
LVL 1

Author Comment

by:Shaye Larsen
ID: 24895871
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:
Pawel Witkowski earned 500 total points
ID: 24896420
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:Shaye Larsen
ID: 31604029
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

628 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