how to trigger code on certain elements? but it doesn't affect others
I want to display an alert if I select the "b" option, but before that, I'm in the "a" option, not from the "c" option.
<select>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
jquery html
|
show 1 more comment
I want to display an alert if I select the "b" option, but before that, I'm in the "a" option, not from the "c" option.
<select>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
jquery html
what JS have you tried thus far?
– treyBake
Nov 26 '18 at 10:37
$("option).click(function(){alert("show")})";
– Dio Satria Darma
Nov 26 '18 at 10:38
Do I understand you correctly, you want to onlyalert
when the user clicks ona
before clicking onb
?
– Jacob Schneider
Nov 26 '18 at 10:40
with nothing inside the function .. ?
– treyBake
Nov 26 '18 at 10:40
there is a function
– Dio Satria Darma
Nov 26 '18 at 10:42
|
show 1 more comment
I want to display an alert if I select the "b" option, but before that, I'm in the "a" option, not from the "c" option.
<select>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
jquery html
I want to display an alert if I select the "b" option, but before that, I'm in the "a" option, not from the "c" option.
<select>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
jquery html
jquery html
edited Nov 26 '18 at 10:47
Nick Parsons
10.2k2926
10.2k2926
asked Nov 26 '18 at 10:32
Dio Satria DarmaDio Satria Darma
74
74
what JS have you tried thus far?
– treyBake
Nov 26 '18 at 10:37
$("option).click(function(){alert("show")})";
– Dio Satria Darma
Nov 26 '18 at 10:38
Do I understand you correctly, you want to onlyalert
when the user clicks ona
before clicking onb
?
– Jacob Schneider
Nov 26 '18 at 10:40
with nothing inside the function .. ?
– treyBake
Nov 26 '18 at 10:40
there is a function
– Dio Satria Darma
Nov 26 '18 at 10:42
|
show 1 more comment
what JS have you tried thus far?
– treyBake
Nov 26 '18 at 10:37
$("option).click(function(){alert("show")})";
– Dio Satria Darma
Nov 26 '18 at 10:38
Do I understand you correctly, you want to onlyalert
when the user clicks ona
before clicking onb
?
– Jacob Schneider
Nov 26 '18 at 10:40
with nothing inside the function .. ?
– treyBake
Nov 26 '18 at 10:40
there is a function
– Dio Satria Darma
Nov 26 '18 at 10:42
what JS have you tried thus far?
– treyBake
Nov 26 '18 at 10:37
what JS have you tried thus far?
– treyBake
Nov 26 '18 at 10:37
$("option).click(function(){alert("show")})";
– Dio Satria Darma
Nov 26 '18 at 10:38
$("option).click(function(){alert("show")})";
– Dio Satria Darma
Nov 26 '18 at 10:38
Do I understand you correctly, you want to only
alert
when the user clicks on a
before clicking on b
?– Jacob Schneider
Nov 26 '18 at 10:40
Do I understand you correctly, you want to only
alert
when the user clicks on a
before clicking on b
?– Jacob Schneider
Nov 26 '18 at 10:40
with nothing inside the function .. ?
– treyBake
Nov 26 '18 at 10:40
with nothing inside the function .. ?
– treyBake
Nov 26 '18 at 10:40
there is a function
– Dio Satria Darma
Nov 26 '18 at 10:42
there is a function
– Dio Satria Darma
Nov 26 '18 at 10:42
|
show 1 more comment
1 Answer
1
active
oldest
votes
You need to create a variable which holds the previous value that you were on. Using this variable you can check whether it was on "a" previously (prev == "a"
).
See working example below:
let prev = $('select').val();
$('select').change(function() {
if(prev == 'a' && $(this).val() == 'b') { // Check if the previous value is 'a' and the current value is 'b'
alert("From a ---> b");
}
prev = $(this).val();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
you don't necessarily need to declare a variable - you can just get value on click, if value is b do action - unless I'm misunderstanding the question
– treyBake
Nov 26 '18 at 10:39
I believe that's the case, to only trigger the event if a previous has occured
– Jacob Schneider
Nov 26 '18 at 10:40
@treyBake I think OP wants it so that if you go from "a" then go to "b" it will display an alert. So if you went from "c" to "b" it wouldn't do anything
– Nick Parsons
Nov 26 '18 at 10:40
@NickParsons hmm I feel like the question is worded too loosely ... maybe I'm just not seeing it as clearly as others ... but hard to tell for me :/
– treyBake
Nov 26 '18 at 10:41
can you simplify the coding? like $ ("option"). click (function () {alert ("a-> b")});
– Dio Satria Darma
Nov 26 '18 at 10:44
|
show 6 more comments
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%2f53479218%2fhow-to-trigger-code-on-certain-elements-but-it-doesnt-affect-others%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
You need to create a variable which holds the previous value that you were on. Using this variable you can check whether it was on "a" previously (prev == "a"
).
See working example below:
let prev = $('select').val();
$('select').change(function() {
if(prev == 'a' && $(this).val() == 'b') { // Check if the previous value is 'a' and the current value is 'b'
alert("From a ---> b");
}
prev = $(this).val();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
you don't necessarily need to declare a variable - you can just get value on click, if value is b do action - unless I'm misunderstanding the question
– treyBake
Nov 26 '18 at 10:39
I believe that's the case, to only trigger the event if a previous has occured
– Jacob Schneider
Nov 26 '18 at 10:40
@treyBake I think OP wants it so that if you go from "a" then go to "b" it will display an alert. So if you went from "c" to "b" it wouldn't do anything
– Nick Parsons
Nov 26 '18 at 10:40
@NickParsons hmm I feel like the question is worded too loosely ... maybe I'm just not seeing it as clearly as others ... but hard to tell for me :/
– treyBake
Nov 26 '18 at 10:41
can you simplify the coding? like $ ("option"). click (function () {alert ("a-> b")});
– Dio Satria Darma
Nov 26 '18 at 10:44
|
show 6 more comments
You need to create a variable which holds the previous value that you were on. Using this variable you can check whether it was on "a" previously (prev == "a"
).
See working example below:
let prev = $('select').val();
$('select').change(function() {
if(prev == 'a' && $(this).val() == 'b') { // Check if the previous value is 'a' and the current value is 'b'
alert("From a ---> b");
}
prev = $(this).val();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
you don't necessarily need to declare a variable - you can just get value on click, if value is b do action - unless I'm misunderstanding the question
– treyBake
Nov 26 '18 at 10:39
I believe that's the case, to only trigger the event if a previous has occured
– Jacob Schneider
Nov 26 '18 at 10:40
@treyBake I think OP wants it so that if you go from "a" then go to "b" it will display an alert. So if you went from "c" to "b" it wouldn't do anything
– Nick Parsons
Nov 26 '18 at 10:40
@NickParsons hmm I feel like the question is worded too loosely ... maybe I'm just not seeing it as clearly as others ... but hard to tell for me :/
– treyBake
Nov 26 '18 at 10:41
can you simplify the coding? like $ ("option"). click (function () {alert ("a-> b")});
– Dio Satria Darma
Nov 26 '18 at 10:44
|
show 6 more comments
You need to create a variable which holds the previous value that you were on. Using this variable you can check whether it was on "a" previously (prev == "a"
).
See working example below:
let prev = $('select').val();
$('select').change(function() {
if(prev == 'a' && $(this).val() == 'b') { // Check if the previous value is 'a' and the current value is 'b'
alert("From a ---> b");
}
prev = $(this).val();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
You need to create a variable which holds the previous value that you were on. Using this variable you can check whether it was on "a" previously (prev == "a"
).
See working example below:
let prev = $('select').val();
$('select').change(function() {
if(prev == 'a' && $(this).val() == 'b') { // Check if the previous value is 'a' and the current value is 'b'
alert("From a ---> b");
}
prev = $(this).val();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
let prev = $('select').val();
$('select').change(function() {
if(prev == 'a' && $(this).val() == 'b') { // Check if the previous value is 'a' and the current value is 'b'
alert("From a ---> b");
}
prev = $(this).val();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
let prev = $('select').val();
$('select').change(function() {
if(prev == 'a' && $(this).val() == 'b') { // Check if the previous value is 'a' and the current value is 'b'
alert("From a ---> b");
}
prev = $(this).val();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
edited Nov 26 '18 at 10:45
answered Nov 26 '18 at 10:37
Nick ParsonsNick Parsons
10.2k2926
10.2k2926
you don't necessarily need to declare a variable - you can just get value on click, if value is b do action - unless I'm misunderstanding the question
– treyBake
Nov 26 '18 at 10:39
I believe that's the case, to only trigger the event if a previous has occured
– Jacob Schneider
Nov 26 '18 at 10:40
@treyBake I think OP wants it so that if you go from "a" then go to "b" it will display an alert. So if you went from "c" to "b" it wouldn't do anything
– Nick Parsons
Nov 26 '18 at 10:40
@NickParsons hmm I feel like the question is worded too loosely ... maybe I'm just not seeing it as clearly as others ... but hard to tell for me :/
– treyBake
Nov 26 '18 at 10:41
can you simplify the coding? like $ ("option"). click (function () {alert ("a-> b")});
– Dio Satria Darma
Nov 26 '18 at 10:44
|
show 6 more comments
you don't necessarily need to declare a variable - you can just get value on click, if value is b do action - unless I'm misunderstanding the question
– treyBake
Nov 26 '18 at 10:39
I believe that's the case, to only trigger the event if a previous has occured
– Jacob Schneider
Nov 26 '18 at 10:40
@treyBake I think OP wants it so that if you go from "a" then go to "b" it will display an alert. So if you went from "c" to "b" it wouldn't do anything
– Nick Parsons
Nov 26 '18 at 10:40
@NickParsons hmm I feel like the question is worded too loosely ... maybe I'm just not seeing it as clearly as others ... but hard to tell for me :/
– treyBake
Nov 26 '18 at 10:41
can you simplify the coding? like $ ("option"). click (function () {alert ("a-> b")});
– Dio Satria Darma
Nov 26 '18 at 10:44
you don't necessarily need to declare a variable - you can just get value on click, if value is b do action - unless I'm misunderstanding the question
– treyBake
Nov 26 '18 at 10:39
you don't necessarily need to declare a variable - you can just get value on click, if value is b do action - unless I'm misunderstanding the question
– treyBake
Nov 26 '18 at 10:39
I believe that's the case, to only trigger the event if a previous has occured
– Jacob Schneider
Nov 26 '18 at 10:40
I believe that's the case, to only trigger the event if a previous has occured
– Jacob Schneider
Nov 26 '18 at 10:40
@treyBake I think OP wants it so that if you go from "a" then go to "b" it will display an alert. So if you went from "c" to "b" it wouldn't do anything
– Nick Parsons
Nov 26 '18 at 10:40
@treyBake I think OP wants it so that if you go from "a" then go to "b" it will display an alert. So if you went from "c" to "b" it wouldn't do anything
– Nick Parsons
Nov 26 '18 at 10:40
@NickParsons hmm I feel like the question is worded too loosely ... maybe I'm just not seeing it as clearly as others ... but hard to tell for me :/
– treyBake
Nov 26 '18 at 10:41
@NickParsons hmm I feel like the question is worded too loosely ... maybe I'm just not seeing it as clearly as others ... but hard to tell for me :/
– treyBake
Nov 26 '18 at 10:41
can you simplify the coding? like $ ("option"). click (function () {alert ("a-> b")});
– Dio Satria Darma
Nov 26 '18 at 10:44
can you simplify the coding? like $ ("option"). click (function () {alert ("a-> b")});
– Dio Satria Darma
Nov 26 '18 at 10:44
|
show 6 more comments
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%2f53479218%2fhow-to-trigger-code-on-certain-elements-but-it-doesnt-affect-others%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
what JS have you tried thus far?
– treyBake
Nov 26 '18 at 10:37
$("option).click(function(){alert("show")})";
– Dio Satria Darma
Nov 26 '18 at 10:38
Do I understand you correctly, you want to only
alert
when the user clicks ona
before clicking onb
?– Jacob Schneider
Nov 26 '18 at 10:40
with nothing inside the function .. ?
– treyBake
Nov 26 '18 at 10:40
there is a function
– Dio Satria Darma
Nov 26 '18 at 10:42