简体   繁体   中英

Javascript asp.net c# multi objet pass to Js code file

I have code on html page and it is working:

document.getElementById('<%=IdOstan.ClientID %>').options.length = 0;

it is part of a function. Object is found and code run. But if I move code to a seperate js file, then call the file using :

<script src="../../../../Script/AjaxCall.js" type="text/javascript"></script>

then object is not found. My conclusion was that Object on the same file can be found with calling document.getElementById , but it is not true about object passed to Js, and seems normal. I have tried to pass object while calling JS function like this :

<asp:DropDownList ID="IdLand" runat="server" onchange="UpdateOstan(this);" 
     DataTextField="NameLand" DataValueField="IdLand" ViewStateMode="Enabled" 
     AutoPostBack="false">
</asp:DropDownList>

and it also works. But I need to pass a second object too I have tried :

onchange="UpdateOstan(this, IdsecondObj);"

which failed, also this

onchange="UpdateOstan(this, document.getElementById('IdSecondObject'));"

not working. So pleas let me know how to pass html object to Javascript which is not on the same page. I don't want to use jQuery files as helper,at all. Thanks

Use hidden input to store ID value of your IdOStan.ClientID and get that value for id name from your external javascript and then you can access that object.

<input id="MyHidden" value="<%=IdOstan.ClientID %>" />

In external JS file.

var id = document.getElementById('MyHidden').value;
var IdOstan = document.getElementById(id);

Now IdOstan is the object you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM