Reputation: 53
I have this function that determines the id of the clicked element. I need to store it as a variable that I can use in another function.
$(".loadlist").click(function() {
var folderid;
folderid =jQuery(this).attr('id') || ''
$("#adults").swap({
target: "table1_wrapper",
opacity: "0.5",
speed: 1000,
});
});
The second function is
jQuery(document).on("click", "#trigger_adults", function (event) {
folderName = "/sites/bi/adultslibrary/";
fullpath = folderName + folderid;
$().SPServices({
webUrl: "http://onespace.nottscc.gov.uk/sites/bihub",
operation: "GetListItems",
async: false,
listName: "adultslibrary",
CAMLRowLimit: 100,
CAMLViewFields: "<ViewFields>
<FieldRef Name='LinkFilename' />
<FieldRef Name='EncodedAbsUrl'/>
<FieldRef Name='Year'/>
<FieldRef Name='Snapshot'/></ViewFields> " ,
CAMLQueryOptions: "<QueryOptions><Folder>"+fullpath+ "</Folder></QueryOptions>",
// everything else
I need to be able to access the folder id variable in the second function so I can use it to concatenate the url path.
Upvotes: 0
Views: 71
Reputation: 793
In addition to defining your variables gloabally, as mentioned in other answers, I believe the issue is that you aren't wrapping your Javascript in a document.onready function.
Anyways, the following worked for me:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<div class="loadlist" id="1">
LoadList
</div>
<br><br>
<div id="trigger_adults">
#trigger_adults
</div>
<script>
$(function() {
var folderid;
var folderName;
var fullpath;
$(".loadlist").click(function() {
folderid = jQuery(this).attr('id') || '';
});
jQuery(document).on("click", "#trigger_adults", function(event) {
folderName = "/sites/bi/adultslibrary/";
fullpath = folderName + folderid;
alert(fullpath);
});
});
</script>
If all else fails, make sure that your version of jQuery is v1.7 or newer.
Upvotes: 3
Reputation: 881
You can define the variable outside the scope of the function invoked when .loadlist
is clicked.
var folderid;
$(".loadlist").click(function() {
folderid =jQuery(this).attr('id') || ''
$("#adults").swap({
target: "table1_wrapper",
opacity: "0.5",
speed: 1000,
});
});
And then your second function:
jQuery(document).on("click", "#trigger_adults", function (event) {
folderName = "/sites/bi/adultslibrary/";
fullpath = folderName + folderid;
//...
You can also see this in action here: JSFiddle
Upvotes: 1
Reputation: 4626
You could use a variable outside of both functions like this:
var mySpecialId;
$(".loadlist").click(function() {
mySpecialId = xxx;
...
});
$().SPServices({
do something with mySpecialId
...
});
Upvotes: 1