Redefine ID with ampersand
Working with Sly's scrolling codes (http://darsa.in/sly/).
Having multiple Sly carousels on a page, I need to fix the ID of the frame
I generate them with '#=basic-XXX', where XXX is the record of the album.
the standard code is this:
var $frame = $('#basic');
var $slidee = $frame.children('ul').eq(0);
var $wrap = $frame.parent();
I try to read the ID, including the attached record number from the database.
var $frame = $("[id^=basic-]"); // start with...
// trying these two lines, but they FAIL
var num = $frame.slice(7);
var $frame = $("#basic-"+num);
//from here $frame should be redefined as #basic-THENUMBER
var $slidee = $frame.children('ul').eq(0);
var $wrap = $frame.parent();
Any idea how I can update var $frame with the ID so it works for the rest of the script?
php jquery
add a comment |
Working with Sly's scrolling codes (http://darsa.in/sly/).
Having multiple Sly carousels on a page, I need to fix the ID of the frame
I generate them with '#=basic-XXX', where XXX is the record of the album.
the standard code is this:
var $frame = $('#basic');
var $slidee = $frame.children('ul').eq(0);
var $wrap = $frame.parent();
I try to read the ID, including the attached record number from the database.
var $frame = $("[id^=basic-]"); // start with...
// trying these two lines, but they FAIL
var num = $frame.slice(7);
var $frame = $("#basic-"+num);
//from here $frame should be redefined as #basic-THENUMBER
var $slidee = $frame.children('ul').eq(0);
var $wrap = $frame.parent();
Any idea how I can update var $frame with the ID so it works for the rest of the script?
php jquery
Is PHP related?
– user3783243
Nov 24 '18 at 1:17
In a way, yes; it is used in an entirely php scripted site.
– KJS
Nov 24 '18 at 1:23
1
It is not related to the script/question you are asking though, right?
– user3783243
Nov 24 '18 at 1:24
You're right. Thanks for keeping me focussed!
– KJS
Nov 25 '18 at 21:09
add a comment |
Working with Sly's scrolling codes (http://darsa.in/sly/).
Having multiple Sly carousels on a page, I need to fix the ID of the frame
I generate them with '#=basic-XXX', where XXX is the record of the album.
the standard code is this:
var $frame = $('#basic');
var $slidee = $frame.children('ul').eq(0);
var $wrap = $frame.parent();
I try to read the ID, including the attached record number from the database.
var $frame = $("[id^=basic-]"); // start with...
// trying these two lines, but they FAIL
var num = $frame.slice(7);
var $frame = $("#basic-"+num);
//from here $frame should be redefined as #basic-THENUMBER
var $slidee = $frame.children('ul').eq(0);
var $wrap = $frame.parent();
Any idea how I can update var $frame with the ID so it works for the rest of the script?
php jquery
Working with Sly's scrolling codes (http://darsa.in/sly/).
Having multiple Sly carousels on a page, I need to fix the ID of the frame
I generate them with '#=basic-XXX', where XXX is the record of the album.
the standard code is this:
var $frame = $('#basic');
var $slidee = $frame.children('ul').eq(0);
var $wrap = $frame.parent();
I try to read the ID, including the attached record number from the database.
var $frame = $("[id^=basic-]"); // start with...
// trying these two lines, but they FAIL
var num = $frame.slice(7);
var $frame = $("#basic-"+num);
//from here $frame should be redefined as #basic-THENUMBER
var $slidee = $frame.children('ul').eq(0);
var $wrap = $frame.parent();
Any idea how I can update var $frame with the ID so it works for the rest of the script?
php jquery
php jquery
asked Nov 24 '18 at 1:14
KJSKJS
5971720
5971720
Is PHP related?
– user3783243
Nov 24 '18 at 1:17
In a way, yes; it is used in an entirely php scripted site.
– KJS
Nov 24 '18 at 1:23
1
It is not related to the script/question you are asking though, right?
– user3783243
Nov 24 '18 at 1:24
You're right. Thanks for keeping me focussed!
– KJS
Nov 25 '18 at 21:09
add a comment |
Is PHP related?
– user3783243
Nov 24 '18 at 1:17
In a way, yes; it is used in an entirely php scripted site.
– KJS
Nov 24 '18 at 1:23
1
It is not related to the script/question you are asking though, right?
– user3783243
Nov 24 '18 at 1:24
You're right. Thanks for keeping me focussed!
– KJS
Nov 25 '18 at 21:09
Is PHP related?
– user3783243
Nov 24 '18 at 1:17
Is PHP related?
– user3783243
Nov 24 '18 at 1:17
In a way, yes; it is used in an entirely php scripted site.
– KJS
Nov 24 '18 at 1:23
In a way, yes; it is used in an entirely php scripted site.
– KJS
Nov 24 '18 at 1:23
1
1
It is not related to the script/question you are asking though, right?
– user3783243
Nov 24 '18 at 1:24
It is not related to the script/question you are asking though, right?
– user3783243
Nov 24 '18 at 1:24
You're right. Thanks for keeping me focussed!
– KJS
Nov 25 '18 at 21:09
You're right. Thanks for keeping me focussed!
– KJS
Nov 25 '18 at 21:09
add a comment |
1 Answer
1
active
oldest
votes
$("[id^=basic-]")
will return the elements that match the selector and you can then use .attr('id')
to get the first element's id value. If there is only one element with an id starting with basic-
then this will work:
$frame = $("[id^=basic-]").attr('id');
Note that when you use String.slice
, character positions start at 0, so I think you probably want:
var num = $frame.slice(6);
See this demo:
var $frame = $("[id^=basic-]").attr('id');
console.log($frame);
var num = $frame.slice(6);
console.log(num);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<span id="basic-355">hello world!</span>
</div>
If you have multiple elements that have matching id's you will need to iterate them using .each
or similar:
var $frames = $("[id^=basic-]");
$frames.each(function () {
let id = $(this).attr('id');
console.log(id);
let num = id.slice(6);
console.log(num);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<span id="basic-355">hello world!</span>
<span id="basic-562">hello world!</span>
</div>
2
$("[id^=basic-]")[0]
return dom element and you can't use.attr('id')
after it
– Mohammad
Nov 24 '18 at 13:38
@Mohammad thanks for pointing that out - I've corrected.
– Nick
Nov 24 '18 at 22:23
Thank you so much @Nick! Your solution works even better than what I eventually came up with. I also used the .each on it, like this: $("[id^=basic-]").each(function() { var num = this.id.slice(6); $frame = $("#basic-"+num);....etc. Will be using yours though! Many thanks!
– KJS
Nov 25 '18 at 21:08
@KJS no worries. Glad I could help.
– Nick
Nov 25 '18 at 22:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454364%2fredefine-id-with-ampersand%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$("[id^=basic-]")
will return the elements that match the selector and you can then use .attr('id')
to get the first element's id value. If there is only one element with an id starting with basic-
then this will work:
$frame = $("[id^=basic-]").attr('id');
Note that when you use String.slice
, character positions start at 0, so I think you probably want:
var num = $frame.slice(6);
See this demo:
var $frame = $("[id^=basic-]").attr('id');
console.log($frame);
var num = $frame.slice(6);
console.log(num);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<span id="basic-355">hello world!</span>
</div>
If you have multiple elements that have matching id's you will need to iterate them using .each
or similar:
var $frames = $("[id^=basic-]");
$frames.each(function () {
let id = $(this).attr('id');
console.log(id);
let num = id.slice(6);
console.log(num);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<span id="basic-355">hello world!</span>
<span id="basic-562">hello world!</span>
</div>
2
$("[id^=basic-]")[0]
return dom element and you can't use.attr('id')
after it
– Mohammad
Nov 24 '18 at 13:38
@Mohammad thanks for pointing that out - I've corrected.
– Nick
Nov 24 '18 at 22:23
Thank you so much @Nick! Your solution works even better than what I eventually came up with. I also used the .each on it, like this: $("[id^=basic-]").each(function() { var num = this.id.slice(6); $frame = $("#basic-"+num);....etc. Will be using yours though! Many thanks!
– KJS
Nov 25 '18 at 21:08
@KJS no worries. Glad I could help.
– Nick
Nov 25 '18 at 22:18
add a comment |
$("[id^=basic-]")
will return the elements that match the selector and you can then use .attr('id')
to get the first element's id value. If there is only one element with an id starting with basic-
then this will work:
$frame = $("[id^=basic-]").attr('id');
Note that when you use String.slice
, character positions start at 0, so I think you probably want:
var num = $frame.slice(6);
See this demo:
var $frame = $("[id^=basic-]").attr('id');
console.log($frame);
var num = $frame.slice(6);
console.log(num);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<span id="basic-355">hello world!</span>
</div>
If you have multiple elements that have matching id's you will need to iterate them using .each
or similar:
var $frames = $("[id^=basic-]");
$frames.each(function () {
let id = $(this).attr('id');
console.log(id);
let num = id.slice(6);
console.log(num);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<span id="basic-355">hello world!</span>
<span id="basic-562">hello world!</span>
</div>
2
$("[id^=basic-]")[0]
return dom element and you can't use.attr('id')
after it
– Mohammad
Nov 24 '18 at 13:38
@Mohammad thanks for pointing that out - I've corrected.
– Nick
Nov 24 '18 at 22:23
Thank you so much @Nick! Your solution works even better than what I eventually came up with. I also used the .each on it, like this: $("[id^=basic-]").each(function() { var num = this.id.slice(6); $frame = $("#basic-"+num);....etc. Will be using yours though! Many thanks!
– KJS
Nov 25 '18 at 21:08
@KJS no worries. Glad I could help.
– Nick
Nov 25 '18 at 22:18
add a comment |
$("[id^=basic-]")
will return the elements that match the selector and you can then use .attr('id')
to get the first element's id value. If there is only one element with an id starting with basic-
then this will work:
$frame = $("[id^=basic-]").attr('id');
Note that when you use String.slice
, character positions start at 0, so I think you probably want:
var num = $frame.slice(6);
See this demo:
var $frame = $("[id^=basic-]").attr('id');
console.log($frame);
var num = $frame.slice(6);
console.log(num);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<span id="basic-355">hello world!</span>
</div>
If you have multiple elements that have matching id's you will need to iterate them using .each
or similar:
var $frames = $("[id^=basic-]");
$frames.each(function () {
let id = $(this).attr('id');
console.log(id);
let num = id.slice(6);
console.log(num);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<span id="basic-355">hello world!</span>
<span id="basic-562">hello world!</span>
</div>
$("[id^=basic-]")
will return the elements that match the selector and you can then use .attr('id')
to get the first element's id value. If there is only one element with an id starting with basic-
then this will work:
$frame = $("[id^=basic-]").attr('id');
Note that when you use String.slice
, character positions start at 0, so I think you probably want:
var num = $frame.slice(6);
See this demo:
var $frame = $("[id^=basic-]").attr('id');
console.log($frame);
var num = $frame.slice(6);
console.log(num);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<span id="basic-355">hello world!</span>
</div>
If you have multiple elements that have matching id's you will need to iterate them using .each
or similar:
var $frames = $("[id^=basic-]");
$frames.each(function () {
let id = $(this).attr('id');
console.log(id);
let num = id.slice(6);
console.log(num);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<span id="basic-355">hello world!</span>
<span id="basic-562">hello world!</span>
</div>
var $frame = $("[id^=basic-]").attr('id');
console.log($frame);
var num = $frame.slice(6);
console.log(num);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<span id="basic-355">hello world!</span>
</div>
var $frame = $("[id^=basic-]").attr('id');
console.log($frame);
var num = $frame.slice(6);
console.log(num);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<span id="basic-355">hello world!</span>
</div>
var $frames = $("[id^=basic-]");
$frames.each(function () {
let id = $(this).attr('id');
console.log(id);
let num = id.slice(6);
console.log(num);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<span id="basic-355">hello world!</span>
<span id="basic-562">hello world!</span>
</div>
var $frames = $("[id^=basic-]");
$frames.each(function () {
let id = $(this).attr('id');
console.log(id);
let num = id.slice(6);
console.log(num);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<span id="basic-355">hello world!</span>
<span id="basic-562">hello world!</span>
</div>
edited Nov 24 '18 at 22:22
answered Nov 24 '18 at 1:49
NickNick
25.1k91735
25.1k91735
2
$("[id^=basic-]")[0]
return dom element and you can't use.attr('id')
after it
– Mohammad
Nov 24 '18 at 13:38
@Mohammad thanks for pointing that out - I've corrected.
– Nick
Nov 24 '18 at 22:23
Thank you so much @Nick! Your solution works even better than what I eventually came up with. I also used the .each on it, like this: $("[id^=basic-]").each(function() { var num = this.id.slice(6); $frame = $("#basic-"+num);....etc. Will be using yours though! Many thanks!
– KJS
Nov 25 '18 at 21:08
@KJS no worries. Glad I could help.
– Nick
Nov 25 '18 at 22:18
add a comment |
2
$("[id^=basic-]")[0]
return dom element and you can't use.attr('id')
after it
– Mohammad
Nov 24 '18 at 13:38
@Mohammad thanks for pointing that out - I've corrected.
– Nick
Nov 24 '18 at 22:23
Thank you so much @Nick! Your solution works even better than what I eventually came up with. I also used the .each on it, like this: $("[id^=basic-]").each(function() { var num = this.id.slice(6); $frame = $("#basic-"+num);....etc. Will be using yours though! Many thanks!
– KJS
Nov 25 '18 at 21:08
@KJS no worries. Glad I could help.
– Nick
Nov 25 '18 at 22:18
2
2
$("[id^=basic-]")[0]
return dom element and you can't use .attr('id')
after it– Mohammad
Nov 24 '18 at 13:38
$("[id^=basic-]")[0]
return dom element and you can't use .attr('id')
after it– Mohammad
Nov 24 '18 at 13:38
@Mohammad thanks for pointing that out - I've corrected.
– Nick
Nov 24 '18 at 22:23
@Mohammad thanks for pointing that out - I've corrected.
– Nick
Nov 24 '18 at 22:23
Thank you so much @Nick! Your solution works even better than what I eventually came up with. I also used the .each on it, like this: $("[id^=basic-]").each(function() { var num = this.id.slice(6); $frame = $("#basic-"+num);....etc. Will be using yours though! Many thanks!
– KJS
Nov 25 '18 at 21:08
Thank you so much @Nick! Your solution works even better than what I eventually came up with. I also used the .each on it, like this: $("[id^=basic-]").each(function() { var num = this.id.slice(6); $frame = $("#basic-"+num);....etc. Will be using yours though! Many thanks!
– KJS
Nov 25 '18 at 21:08
@KJS no worries. Glad I could help.
– Nick
Nov 25 '18 at 22:18
@KJS no worries. Glad I could help.
– Nick
Nov 25 '18 at 22:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454364%2fredefine-id-with-ampersand%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is PHP related?
– user3783243
Nov 24 '18 at 1:17
In a way, yes; it is used in an entirely php scripted site.
– KJS
Nov 24 '18 at 1:23
1
It is not related to the script/question you are asking though, right?
– user3783243
Nov 24 '18 at 1:24
You're right. Thanks for keeping me focussed!
– KJS
Nov 25 '18 at 21:09