Solved

javascript function with and without parameters

Posted on 2014-01-23
21
212 Views
Last Modified: 2014-01-27
Please see the attached file.
Should the first function work identically to the second?  If not why the difference?
The second function has the name of the list box passed as a paramenter, but I want it to work the same as the first function.
Code.txt
0
Comment
Question by:AlHal2
  • 10
  • 7
  • 4
21 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39803706
If listBox2 is being passed as an object then there is no difference
0
 

Author Comment

by:AlHal2
ID: 39803713
How do I check if its being passed as an object?
What else could it be passed as?
0
 
LVL 58

Expert Comment

by:Gary
ID: 39803719
I don't know, you've only given us the function/s - where is the code that calls the function?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 58

Expert Comment

by:Gary
ID: 39803734
Quick example passing as an object

http://jsfiddle.net/GaryC123/6HbL3/

This will give an alert of the number of options, obviously the rest of the code will not work as I don't have all your code.
0
 

Author Comment

by:AlHal2
ID: 39803747
I have 2 similar functions.  One function is called like this.


Partial Public Class SuppliedFile
 

    Inherits System.Web.UI.Page
    Protected cmbFileTypeCode As System.Web.UI.WebControls.DropDownList
    Protected lstFileTypeCode As System.Web.UI.WebControls.ListBox
    Protected hidfiletypecodecmb As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected hidfiletypecodelst As System.Web.UI.HtmlControls.HtmlInputHidden


This is in form load
        Me.lstFileTypeCode.Attributes.Add("onchange", "JavaScript:OnListRemove('ctl00_ContentPlaceHolder1_lstfiletypecode', 'ctl00_ContentPlaceHolder1_cmbfiletypecode', 'ctl00_ContentPlaceHolder1_hidfiletypecodecmb', 'ctl00_ContentPlaceHolder1_hidfiletypecodelst')")


The other function is called like this.

<asp:DropDownList ID="cmbFileTypeCode" runat="server"
OnChange ="Javascript:OnDropDownSelect('ctl00_ContentPlaceHolder1_lstFileTypeCode','ctl00_ContentPlaceHolder1_cmbFileTypeCode','ctl00_ContentPlaceHolder1_hidfiletypecodecmb','ctl00_ContentPlaceHolder1_hidfiletypecodelst');"
AppendDataBoundItems="True" AutoPostBack="True"></asp:DropDownList>
0
 

Author Comment

by:AlHal2
ID: 39803765
Here is the HTML for the controls.

    <input id="hidfiletypecodecmb"  name ="hidfiletypecodecmb" type="hidden" runat ="server" />
    <input id="hidfiletypecodelst" name ="hidfiletypecodelst" type="hidden" runat ="server"/>
<asp:ListBox ID="lstFileTypeCode" runat="server" ></asp:ListBox>
0
 
LVL 58

Expert Comment

by:Gary
ID: 39803779
I don't see how that code relates to your js functions.
0
 
LVL 43

Expert Comment

by:Rob
ID: 39804814
What you're trying to do is function overloading, which isn't fully supported in JavaScript. What you should do is get rid of the second function and write all your code in the first.
I would review the need to have two functions with the same name. Why do you need both?
0
 

Author Comment

by:AlHal2
ID: 39805969
There is only one function of that name.  I copied and pasted it, so I could add the differences.  I have several listboxes on the page and want to call the function for each.  Therefore I was trying to pass the listbox name as a parameter.
0
 
LVL 43

Expert Comment

by:Rob
ID: 39805997
That sounds ok. But you removed the old function so that you just have the one function passing the parameter each time?
So what's not happening as you expect?
0
 

Author Comment

by:AlHal2
ID: 39806107
My reply is in the attached file as I get an error message when I tried to submit it.
Code2.doc
0
 
LVL 43

Expert Comment

by:Rob
ID: 39808127
Can you post the code for the whole rendered page? To do this, view it in a browser, right click, view source.  Copy and post that here.

I can't see what the page is doing with part .NET and part javascript
0
 

Author Comment

by:AlHal2
ID: 39811410
I get told that I can't start and i tag within an i tag, so I attach it.
Thanks for taking the time to look.
I think the fact that I'm using a master page is significant.
RenderedPage.txt
0
 
LVL 43

Expert Comment

by:Rob
ID: 39811508
If I understand correctly you want the two functions in this post http:#a39803747 to do the same thing yeah?

The first event calls the javascript function OnListRemove(), the second calls the javascript function OnDropDownSelect().

You're passing the same arguments to each so it depends on the content of each function.  If they're doing exactly the same thing then change them to a common name rather than OnListRemove and OnDropDownSelect.

The following will allow you to perform the same action but also one or both may need individual code to run before the common code is executed in doWhatIWant():

function OnDropDownSelect('ctl00_ContentPlaceHolder1_lstFileTypeCode','ctl00_ContentPlaceHolder1_cmbFileTypeCode','ctl00_ContentPlaceHolder1_hidfiletypecodecmb','ctl00_ContentPlaceHolder1_hidfiletypecodelst') {
    doWhatIWant('ctl00_ContentPlaceHolder1_lstfiletypecode', 'ctl00_ContentPlaceHolder1_cmbfiletypecode', 'ctl00_ContentPlaceHolder1_hidfiletypecodecmb', 'ctl00_ContentPlaceHolder1_hidfiletypecodelst');
}
function OnListRemove('ctl00_ContentPlaceHolder1_lstfiletypecode', 'ctl00_ContentPlaceHolder1_cmbfiletypecode', 'ctl00_ContentPlaceHolder1_hidfiletypecodecmb', 'ctl00_ContentPlaceHolder1_hidfiletypecodelst') {
    doWhatIWant('ctl00_ContentPlaceHolder1_lstfiletypecode', 'ctl00_ContentPlaceHolder1_cmbfiletypecode', 'ctl00_ContentPlaceHolder1_hidfiletypecodecmb', 'ctl00_ContentPlaceHolder1_hidfiletypecodelst');
}

Open in new window


now create a single function:

function doWhatIWant('ctl00_ContentPlaceHolder1_lstfiletypecode', 'ctl00_ContentPlaceHolder1_cmbfiletypecode', 'ctl00_ContentPlaceHolder1_hidfiletypecodecmb', 'ctl00_ContentPlaceHolder1_hidfiletypecodelst') {
    // do common actions here
}

Open in new window

0
 

Author Comment

by:AlHal2
ID: 39811547
The function perform opposite actions.
When the user clicks a drop down box the function OnDropDownSelect() should add the selected item to the corresponding listbox and remove it from the dropdown box.
When the user selects an item in the listbox the function OnListRemove() should add the item to the drop down box and remove it from the list box.
Since the listbox doesn't have a click event I had to add this attribute in the code behind page.
0
 

Author Comment

by:AlHal2
ID: 39811563
The function perform opposite actions.
should be
The functions perform opposite actions.
0
 
LVL 43

Expert Comment

by:Rob
ID: 39811568
A drop down and a listbox are the same element where the listbox has an extra attribute "size"

eg
http://jsbin.com/EsINOke/1/edit?html,output

In other words it should support the click event
0
 
LVL 43

Expert Comment

by:Rob
ID: 39811571
I'm also confused as to what you're question is as the question says you want them to perform the same

Should the first function work identically to the second?  If not why the difference?
The second function has the name of the list box passed as a paramenter, but I want it to work the same as the first function.
0
 

Author Comment

by:AlHal2
ID: 39811593
When I wrote that post I thought the problem was that the functions were working differently because one had the listbox and dropdown box as parameters whereas the other did not.  Therefore I was trying to clarify that point and made my example functions identical in all other respects.

When you says A drop down and a listbox are the same element, are you referring to asp dropdownlists and lisboxes or the HTML dropdown box?  I'm using ASP controls perhaps incorrectly.
0
 
LVL 43

Accepted Solution

by:
Rob earned 250 total points
ID: 39811641
I don't think  you're misusing the controls but yes I was referring to the HTML dropdown.  <asp:ListBox...> does render as a <select> element and will therefore trigger an onchange event

http://jsbin.com/EsINOke/2/edit?html,output
0
 

Author Closing Comment

by:AlHal2
ID: 39812527
Thanks.  If I need more help I'll post a separate question.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

828 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