How to use a variable inside $('#id option:eq(VARIABLE)')?
I basically want to set some dropdown menu to a certain index by comparing to the respective value.
Here's the code:
function InitializeLieblingsplatzOptions(){
$.post('../include/retrieveLieblingsplatz.php',{
//nothing to transmit
}).then((data) => {
data = JSON.parse(data)
console.log("data in retrieveLieblingsplatz.php is ", data)
//let test = $('#liebraum').find("test")
$('#liebraum option:eq(data.raum)').prop('selected', true)
$('#liebsitz option:eq(data.nummer)').prop('selected', true)
})
}
the data returned from the ajax contains two strings which represent a roomname and seatnumber. There are 2 dropdown menus on this site which contain all names of all rooms and all available seats of the respective room.
I now want to use the 2 strings returned by the AJAX to set the options of these dropdown menus to the respective options. Therefore I'm using these two jquery commands already displayed above:
$('#liebraum option:eq(data.raum)').prop('selected', true)
$('#liebsitz option:eq(data.nummer)').prop('selected', true)
Currently, nothing is happening ^^ Its like the two dropwdown menus are not even touched. I wonder if this is because I have to put these variables containing the strings to compare to into the " '' " clause. Because of this they are just regarded as strings, not as variables. I never had such a case so far in the context of jquery and I have no idea if there is a way to deal with it Oo
jquery
add a comment |
I basically want to set some dropdown menu to a certain index by comparing to the respective value.
Here's the code:
function InitializeLieblingsplatzOptions(){
$.post('../include/retrieveLieblingsplatz.php',{
//nothing to transmit
}).then((data) => {
data = JSON.parse(data)
console.log("data in retrieveLieblingsplatz.php is ", data)
//let test = $('#liebraum').find("test")
$('#liebraum option:eq(data.raum)').prop('selected', true)
$('#liebsitz option:eq(data.nummer)').prop('selected', true)
})
}
the data returned from the ajax contains two strings which represent a roomname and seatnumber. There are 2 dropdown menus on this site which contain all names of all rooms and all available seats of the respective room.
I now want to use the 2 strings returned by the AJAX to set the options of these dropdown menus to the respective options. Therefore I'm using these two jquery commands already displayed above:
$('#liebraum option:eq(data.raum)').prop('selected', true)
$('#liebsitz option:eq(data.nummer)').prop('selected', true)
Currently, nothing is happening ^^ Its like the two dropwdown menus are not even touched. I wonder if this is because I have to put these variables containing the strings to compare to into the " '' " clause. Because of this they are just regarded as strings, not as variables. I never had such a case so far in the context of jquery and I have no idea if there is a way to deal with it Oo
jquery
add a comment |
I basically want to set some dropdown menu to a certain index by comparing to the respective value.
Here's the code:
function InitializeLieblingsplatzOptions(){
$.post('../include/retrieveLieblingsplatz.php',{
//nothing to transmit
}).then((data) => {
data = JSON.parse(data)
console.log("data in retrieveLieblingsplatz.php is ", data)
//let test = $('#liebraum').find("test")
$('#liebraum option:eq(data.raum)').prop('selected', true)
$('#liebsitz option:eq(data.nummer)').prop('selected', true)
})
}
the data returned from the ajax contains two strings which represent a roomname and seatnumber. There are 2 dropdown menus on this site which contain all names of all rooms and all available seats of the respective room.
I now want to use the 2 strings returned by the AJAX to set the options of these dropdown menus to the respective options. Therefore I'm using these two jquery commands already displayed above:
$('#liebraum option:eq(data.raum)').prop('selected', true)
$('#liebsitz option:eq(data.nummer)').prop('selected', true)
Currently, nothing is happening ^^ Its like the two dropwdown menus are not even touched. I wonder if this is because I have to put these variables containing the strings to compare to into the " '' " clause. Because of this they are just regarded as strings, not as variables. I never had such a case so far in the context of jquery and I have no idea if there is a way to deal with it Oo
jquery
I basically want to set some dropdown menu to a certain index by comparing to the respective value.
Here's the code:
function InitializeLieblingsplatzOptions(){
$.post('../include/retrieveLieblingsplatz.php',{
//nothing to transmit
}).then((data) => {
data = JSON.parse(data)
console.log("data in retrieveLieblingsplatz.php is ", data)
//let test = $('#liebraum').find("test")
$('#liebraum option:eq(data.raum)').prop('selected', true)
$('#liebsitz option:eq(data.nummer)').prop('selected', true)
})
}
the data returned from the ajax contains two strings which represent a roomname and seatnumber. There are 2 dropdown menus on this site which contain all names of all rooms and all available seats of the respective room.
I now want to use the 2 strings returned by the AJAX to set the options of these dropdown menus to the respective options. Therefore I'm using these two jquery commands already displayed above:
$('#liebraum option:eq(data.raum)').prop('selected', true)
$('#liebsitz option:eq(data.nummer)').prop('selected', true)
Currently, nothing is happening ^^ Its like the two dropwdown menus are not even touched. I wonder if this is because I have to put these variables containing the strings to compare to into the " '' " clause. Because of this they are just regarded as strings, not as variables. I never had such a case so far in the context of jquery and I have no idea if there is a way to deal with it Oo
function InitializeLieblingsplatzOptions(){
$.post('../include/retrieveLieblingsplatz.php',{
//nothing to transmit
}).then((data) => {
data = JSON.parse(data)
console.log("data in retrieveLieblingsplatz.php is ", data)
//let test = $('#liebraum').find("test")
$('#liebraum option:eq(data.raum)').prop('selected', true)
$('#liebsitz option:eq(data.nummer)').prop('selected', true)
})
}
function InitializeLieblingsplatzOptions(){
$.post('../include/retrieveLieblingsplatz.php',{
//nothing to transmit
}).then((data) => {
data = JSON.parse(data)
console.log("data in retrieveLieblingsplatz.php is ", data)
//let test = $('#liebraum').find("test")
$('#liebraum option:eq(data.raum)').prop('selected', true)
$('#liebsitz option:eq(data.nummer)').prop('selected', true)
})
}
$('#liebraum option:eq(data.raum)').prop('selected', true)
$('#liebsitz option:eq(data.nummer)').prop('selected', true)
$('#liebraum option:eq(data.raum)').prop('selected', true)
$('#liebsitz option:eq(data.nummer)').prop('selected', true)
jquery
jquery
asked Nov 15 '18 at 10:12
JSONBUG123JSONBUG123
6110
6110
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can do it by using the css selector like so
$('#liebraum option:eq('+data.raum+')').prop('selected', true);
or by using the .eq method exposed by jQuery.
$('#liebraum option').eq(data.raum).prop('selected', true);
add a comment |
You can use ES6 template literals:
$(`#liebraum option:eq(${data.raum})`)
Or, if you want to stick to ES5, use String
concatenation:
$('#liebraum option:eq('+data.raum+')')
add a comment |
Please search how to concatenate strings in javascript...
$('#yourID option:eq(' + yourVariable + ')')
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%2f53317035%2fhow-to-use-a-variable-inside-id-optioneqvariable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do it by using the css selector like so
$('#liebraum option:eq('+data.raum+')').prop('selected', true);
or by using the .eq method exposed by jQuery.
$('#liebraum option').eq(data.raum).prop('selected', true);
add a comment |
You can do it by using the css selector like so
$('#liebraum option:eq('+data.raum+')').prop('selected', true);
or by using the .eq method exposed by jQuery.
$('#liebraum option').eq(data.raum).prop('selected', true);
add a comment |
You can do it by using the css selector like so
$('#liebraum option:eq('+data.raum+')').prop('selected', true);
or by using the .eq method exposed by jQuery.
$('#liebraum option').eq(data.raum).prop('selected', true);
You can do it by using the css selector like so
$('#liebraum option:eq('+data.raum+')').prop('selected', true);
or by using the .eq method exposed by jQuery.
$('#liebraum option').eq(data.raum).prop('selected', true);
answered Nov 15 '18 at 10:19
Miguel M.Miguel M.
367312
367312
add a comment |
add a comment |
You can use ES6 template literals:
$(`#liebraum option:eq(${data.raum})`)
Or, if you want to stick to ES5, use String
concatenation:
$('#liebraum option:eq('+data.raum+')')
add a comment |
You can use ES6 template literals:
$(`#liebraum option:eq(${data.raum})`)
Or, if you want to stick to ES5, use String
concatenation:
$('#liebraum option:eq('+data.raum+')')
add a comment |
You can use ES6 template literals:
$(`#liebraum option:eq(${data.raum})`)
Or, if you want to stick to ES5, use String
concatenation:
$('#liebraum option:eq('+data.raum+')')
You can use ES6 template literals:
$(`#liebraum option:eq(${data.raum})`)
Or, if you want to stick to ES5, use String
concatenation:
$('#liebraum option:eq('+data.raum+')')
answered Nov 15 '18 at 10:16
connexoconnexo
22.5k83561
22.5k83561
add a comment |
add a comment |
Please search how to concatenate strings in javascript...
$('#yourID option:eq(' + yourVariable + ')')
add a comment |
Please search how to concatenate strings in javascript...
$('#yourID option:eq(' + yourVariable + ')')
add a comment |
Please search how to concatenate strings in javascript...
$('#yourID option:eq(' + yourVariable + ')')
Please search how to concatenate strings in javascript...
$('#yourID option:eq(' + yourVariable + ')')
answered Nov 15 '18 at 10:16
Jean-Marc ZimmerJean-Marc Zimmer
38414
38414
add a comment |
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%2f53317035%2fhow-to-use-a-variable-inside-id-optioneqvariable%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