We help IT Professionals succeed at work.

select all values from dropdown list

lulu50
lulu50 asked
on
628 Views
Last Modified: 2014-05-16
HI,

I want to be able to loop thru my dropdown list and send all values as a list.


Thanks,
Lulu

<select name="SelectDepartmentImpacted" class="SearchBySelect5" id="SelectDepartmentImpacted" style="width: 250px; height: 100px; background-color: rgb(248, 250, 252);" size="180" multiple="" sizzle-1400165879525="[object Object]">

<OPTION value=4 data-DepartmentName="BCN – Commons Customer Service">BCN – Commons Customer Service</OPTION>
<OPTION value=3 data-DepartmentName="BCN">BCN</OPTION>
<OPTION value=7 data-DepartmentName="Customer Service">Customer Service</OPTION>
</select>



		function ResetDepartment ()
	{
		var ResetDept = 'Clear Department';
		var dropdown = $("#DispDepartmentBox");	
			
		var SelDept = $("#SelectDepartmentImpacted option");
		var listDept = "";
		
	$.each(SelDept, function(index, value) {
		if ($('#SelDept').length > 0) 
		{
			//I want to have a list of all my value so I can send it to the URL
			listDept = ? //I want all the list value from the dropdown list like this listDept = (4,3,2,5)
			
			}
	});
		
		
		$('#TxtDepartment').val('');
		
		$.get( "getDepartmentName.cfm?listDept=" + encodeURIComponent(listDept) + "filter=" + encodeURIComponent(ResetDept), function (data) {
			dropdown.html( data );
		});
	}	

Open in new window

Comment
Watch Question

Top Expert 2014

Commented:
Try as below...

function ResetDepartment() {
    var ResetDept = 'Clear Department';
    var dropdown = $("#DispDepartmentBox");

    var SelDept = $("#SelectDepartmentImpacted option");
    var listDept = [];

    $.each(SelDept, function (index, value) {
        if ($('#SelectDepartmentImpacted').length > 0) {
            //I want to have a list of all my value so I can send it to the URL
            listDept.push($(value).val()); //I want all the list value from the dropdown list like this listDept = (4,3,2,5)

        }
    });

    var output = JSON.stringify(listDept);

    $('#TxtDepartment').val('');

    $.get("getDepartmentName.cfm?listDept=" + encodeURIComponent(output) + "filter=" + encodeURIComponent(ResetDept), function (data) {
        dropdown.html(data);
    });
}

Open in new window

Play here - http://jsfiddle.net/mdshahbrains/7rdY7/1/

Hope that helps :)

Shah
Top Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
lulu50Web application

Author

Commented:
My Keyup event is not working

am I missing something?


$("#TxtDepartment").keyup(function(evt) {
		
	 	var SelDept = $("#SelectDepartmentImpacted option");
    	var listDept = '(';

    	$.each(SelDept, function (index, value) {
        if ($('#SelectDepartmentImpacted').length > 0) {
            listDept += $(value).val() + ',' ; 
        }
    	});
    
    	listDept = listDept.substring(0,listDept.length-1) + ')';

	//URL encoded  encodeURIComponent(myUrl)
		  $.get("getDepartmentName.cfm?listDept=" + encodeURIComponent(listDept) + "filter=" + encodeURIComponent($(this).val()), function (data) {
        dropdown.html(data);
    });
	
	});
	

Open in new window

lulu50Web application

Author

Commented:
This function works fine but my key event is not working

function ResetDepartment ()
	{
		
		var ResetDept = 'Clear Department';
    var dropdown = $("#DispDepartmentBox");

    var SelDept = $("#SelectDepartmentImpacted option");
    var listDept = '(';

    $.each(SelDept, function (index, value) {
        if ($('#SelectDepartmentImpacted').length > 0) {
            
            listDept += $(value).val() + ',' ; 
        }
    });
    
    listDept = listDept.substring(0,listDept.length-1) + ')';

    alert(listDept);


    $('#TxtDepartment').val('');

    $.get("getDepartmentName.cfm?listDept=" + encodeURIComponent(listDept) + "filter=" + encodeURIComponent(ResetDept), function (data) {
        dropdown.html(data);
    });
		
		
		
	}	

Open in new window

Top Expert 2014

Commented:
My Keyup event is not working

am I missing something?

What do you mean by this? In your question you have provided as JS function.

1) Are you getting any JS error? If so, what it says?
2) Is the control going inside keyup event function, can you keep alert inside like this & check if it is being fired?
$("#TxtDepartment").keyup(function(evt) {
    
    alert('Inside kepup event');

    // you remaining code
    ...
});

Open in new window


Cheers,
Shah Md
lulu50Web application

Author

Commented:
Md Shah,

Sorry, if I confused you.

I have the function which works fine and I also have a keyup event.

when the user enter something in a textbox it should filter their entry.

I did put an alert for testing. I do see my alert.
lulu50Web application

Author

Commented:
I am not getting any error but it's not filtering anything.

Let me do some more debugging .

the keyup event should do the same thing as the function.
Top Expert 2014

Commented:
Its okay. I am also trying to understand your issue here..

So the question related stuff has been answered.. This is a bit new issue ahead of it..

Ok lets see what I can help you on this then..

As an initial start.. Replace your keyup event with below code:
$("#TxtDepartment").keyup(function(evt) {

	alert('1. Key up Event fired');		
	var SelDept = $("#SelectDepartmentImpacted option");
    	var listDept = '(';

    	$.each(SelDept, function (index, value) {
	        if ($('#SelectDepartmentImpacted').length > 0) {
        	    listDept += $(value).val() + ',' ; 
	        }
    	});
    
    	listDept = listDept.substring(0,listDept.length-1) + ')';
	
	alert('2. Dropdown List Values : ' + listDept);		
	alert('3. Text Entered, Filter : ' + $(this).val());		

	//URL encoded  encodeURIComponent(myUrl)
	$.get("getDepartmentName.cfm?listDept=" + encodeURIComponent(listDept) + "filter=" + encodeURIComponent($(this).val()), function (data) {

		alert('4. GET function on success : ' + data);		
	       dropdown.html(data);
	});	
});

Open in new window

I have kept 4 ALERTS in the code, now run & let me know what you can see when you enter filter value in TxtDepartment
Top Expert 2014

Commented:
You there..????

Have you tried as said above, whats the output ?

Its already late.. I will be going to sleep shortly..

Let me know if you require any help.

Shah
lulu50Web application

Author

Commented:
I think the problem is with this (encodeURIComponent)  

Can I do that to a list?

<cfset UlistDept = urlDecode(URL.listDept, "utf-8")> 


$.get( "getDepartmentName.cfm?listDept=" + encodeURIComponent(listDept) + "&filter=" + encodeURIComponent($(this).val()), function (data) {
			dropdown.html( data );

Open in new window

lulu50Web application

Author

Commented:
Md Shah,

we can do it tomorrow, if you are tired and need to go to bed.

but, don't forget about me tomorrow, I need to get working.

so, I need your help.

Good night,
Lulu
Top Expert 2014

Commented:
What happened to above alert code I have given in my earlier post.

Are you not getting alert inside function(data) {

As you say if (encodeURIComponent) is the problem then how come it has worked earlier to keyup event when its just a JS function. So problem might be something else.
Top Expert 2014

Commented:
Try to perform $.get without encodeURIComponent, something like below and see if it works:
$.get( "getDepartmentName.cfm?listDept=" + listDept + "&filter=" + $(this).val(), function (data) {
			dropdown.html( data );

Open in new window

Let me know what this results in ???

Shah
Top Expert 2014

Commented:
Also try to post the structure of calling steps where you are fetching values (listDept,filter) from query string in getDepartmentName.cfm file as there can be a issue at that location too. Debug and check what values is being received.

Shah
lulu50Web application

Author

Commented:
Md Shah,

I am debugging the issue now.  

please, don't go anywhere.
lulu50Web application

Author

Commented:
Md Shah,

I am all good now!!!!!!!!!

I got it to work!!!!!!!!

Thank you so much for all your help.

Could not have done it without you.

Thank you again.

Lulu
lulu50Web application

Author

Commented:
Thank you
Top Expert 2014

Commented:
Happy That I have been useful..

Good Luck for further coding :)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.