Reputation: 141
I am trying to load a template to a given DIV in my main .html file. I am trying to use jquery:
$('#Selected').load("template.html",{REQUIREDID:PARENTID})
The template loads correctly but I am unable to pass the variable and use it in the template. For example <div>"requiredid"</div>
How can I read the parameter that I am passing when I load the template
Thanks.
Upvotes: 1
Views: 5263
Reputation: 1391
You should do like this:
GET:
$('#Selected').load("template.html?requiredid=VALUE_TO_PASS");
POST:
$('#Selected').load("template.html",{requiredid=VALUE_TO_PASS});
Add following javascript method inside template.html
to read the query parameters:
function GetURLParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
and use it like:
$('#someDivId').html(GetURLParameter('requiredid'));
I hope, It will help.
Upvotes: 2
Reputation: 1362
You need it as GET parameter and access it with window.location.search as you can see here How to get "GET" request parameters in JavaScript?
Or in a more better way: Place JavaScript code inside the template.html
and use global variables from there. If the template.html
is included in your div container, you can run inside this file JavaScript code as it would be in the main template file from where you called the $.load
function. So, in other words: The scope is the same. Inform yourself about JavaScript scopes.
Edit: That´s a typically angular/react use case. Consider to use on of these MVVM JavaScript frameworks.
Upvotes: 1
Reputation: 495
You have to return the Value for "requiredid" from the Server and store it in a variable. You can then access it in your Callbackfunction after load is completed.
Upvotes: 1