Clear my list

Hi,

I have this jquery that it's not clearing my list before it populate it with a new data.

list = '';  // this is not working

it keeps the previous selection that I want to be removed.

Something is wrong with the code but I'm not sure how to solve it.

 function DetailDialog(w, h,type)
	 {	
		w = 820;
		var left = (screen.width - w) / 2;
        var top = (screen.height - h) / 4; 

	  	var FromDate = $("#GetDateFrom").val();
		var ToDate = $("#GetDateTo").val();

        if (jQuery.cookie('CookieDateFromRpt')){
			$.removeCookie("CookieDateFromRpt");
		}
			$.cookie('CookieDateFromRpt', FromDate, { expires: 10 } );
		
		if (jQuery.cookie('CookieGetDateToRpt')){
			$.removeCookie("CookieGetDateToRpt");
		}
			$.cookie('CookieGetDateToRpt', ToDate, { expires: 10 } );
						
	     var GetValType = $('input[name=CategoryType]:checked', '#ReptFrm').val()
		 var SelName = $("#SelectName option");
		 var SelDept = $("#SelectDepartment option");
		 var SelSeg = $("#SelectSegment option");
         var list = '';
		
		if (GetValType == 'Name')
		{
			list = '';
	    $.each(SelName, function (index, value) {
        if ($('#SelectName').length > 0) {
            
            list += $(value).val() + ',' ; 
        }
        });
    
         list = list.substring(0,list.length-1) + '';

		}else if (GetValType == 'Department')
		{
			list = '';
			 $.each(SelDept, function (index, value) {
        if ($('#SelectDepartment').length > 0) {
            
            list += $(value).val() + ',' ; 
        }
        });
    
         list = list.substring(0,list.length-1) + '';
			
			
		}else if (GetValType == 'Segment')
		{
			list = '';
			 $.each(SelSeg, function (index, value) {
        if ($('#SelectSegment').length > 0) {
            
            list += $(value).val() + ',' ; 
        }
        });
    
         list = list.substring(0,list.length-1) + ''; 
		}	
			
			alert(list);
			
        var targetWin = window.open('OpenCloseDetailRpt.cfm?filter=1&TechType=' + type , 'Group' , 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=1, resizable=1, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left); 

	 }
	 

Open in new window

lulu50Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
Code looks fine to me
What do you mean by
keeps the previous selection
0
lulu50Author Commented:
Gary,

Please, take a look at the picture I only selected one option but my list gives me more numbers
pic2.png
0
GaryCommented:
Your code is looping each element in the dropdown
If you are wanting just the selected elements then change your if else block to

	list = ',';
	if (GetValType == 'Name') {
		$(":selected",SelName).each(function(){
			list += $(value).val() + ',';
		})
	}
	else if (GetValType == 'Department') {
		$(":selected",SelDept).each(function(){
			list += $(value).val() + ',';
		})
	}
	else if (GetValType == 'Segment') {
		$(":selected",SelSeg).each(function(){
			list += $(value).val() + ',';
		})
	}
	list = list.substring(1);

	alert(list);

Open in new window

(condensed the repeating code)
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

lulu50Author Commented:
Gary,

my alert message list shows nothing now.

just like in the picture.


 function DetailDialog(w, h,type)
	 {	
		w = 820;
		var left = (screen.width - w) / 2;
        var top = (screen.height - h) / 4; 

	  	var FromDate = $("#GetDateFrom").val();
		var ToDate = $("#GetDateTo").val();

        if (jQuery.cookie('CookieDateFromRpt')){
			$.removeCookie("CookieDateFromRpt");
		}
			$.cookie('CookieDateFromRpt', FromDate, { expires: 10 } );
		
		if (jQuery.cookie('CookieGetDateToRpt')){
			$.removeCookie("CookieGetDateToRpt");
		}
			$.cookie('CookieGetDateToRpt', ToDate, { expires: 10 } );
						
	     var GetValType = $('input[name=CategoryType]:checked', '#ReptFrm').val()
		 var SelName = $("#SelectName option");
		 var SelDept = $("#SelectDepartment option");
		 var SelSeg = $("#SelectSegment option");
         var list = '';
		
		alert(GetValType);
		
		
		list = ',';
	if (GetValType == 'Name') {
		$(":selected",SelName).each(function(){
			list += $(value).val() + ',';
		})
	}
	else if (GetValType == 'Department') {
		$(":selected",SelDept).each(function(){
			list += $(value).val() + ',';
		})
	}
	else if (GetValType == 'Segment') {
		$(":selected",SelSeg).each(function(){
			list += $(value).val() + ',';
		})
	}
	list = list.substring(1);

	alert(list);

        var targetWin = window.open('OpenCloseDetailRpt.cfm?filter=1&TechType=' + type , 'Group' , 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=1, resizable=1, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left); 

	 }

Open in new window

pic2.png
0
GaryCommented:
Couple of errors in my code

 function DetailDialog(w, h, type) {
     w = 820;
     var left = (screen.width - w) / 2;
     var top = (screen.height - h) / 4;

     var FromDate = $("#GetDateFrom").val();
     var ToDate = $("#GetDateTo").val();

     if (jQuery.cookie('CookieDateFromRpt')) {
         $.removeCookie("CookieDateFromRpt");
     }
     $.cookie('CookieDateFromRpt', FromDate, {
         expires: 10
     });

     if (jQuery.cookie('CookieGetDateToRpt')) {
         $.removeCookie("CookieGetDateToRpt");
     }
     $.cookie('CookieGetDateToRpt', ToDate, {
         expires: 10
     });

     var GetValType = $('input[name=CategoryType]:checked', '#ReptFrm').val()
     var SelName = $("#SelectName");
     var SelDept = $("#SelectDepartment");
     var SelSeg = $("#SelectSegment");


     list = '';
     if (GetValType == 'Name') {
         $(" :selected", SelName).each(function () {
             list += "," + $(this).val();
         })
     } else if (GetValType == 'Department') {
         $(" :selected", SelDept).each(function () {
             list += "," + $(this).val();
         })
     } else if (GetValType == 'Segment') {
         $(" :selected", SelSeg).each(function () {
             list += "," + $(this).val();
         })
     }
     list = list.substring(1);

     alert(list);



     var targetWin = window.open('OpenCloseDetailRpt.cfm?filter=1&TechType=' + type, 'Group', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=1, resizable=1, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);

 }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lulu50Author Commented:
This is great Gary!!!!!!!

Thank  you for your help!!!!!!!!!
0
lulu50Author Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.