Multiple Functions on a .on() without impacting scope
up vote
0
down vote
favorite
I am currently making a dynamic webpage and trying to run multiple functions whenever the element with the class=application is clicked. The below code works but it's very repetitive.
$(document).on("click", ".application", renderImage);
$(document).on("click", ".application", renderText);
$(document).on("click", ".application", renderCodeButton);
$(document).on("click", ".application", renderAppButton);
I attempted to put these functions within one master function and run that with the .on() however as I use $(this).attr to pull information nesting the render functions within a master function ruins the scope.
Here is the renderImage function, though they are all very similar:
function renderImage () {
$(".appPicture").empty();
console.log(this);
let applicationPic = $(this).attr("data-image")
let img = $("<img>");
img.addClass("appPic")
img.attr("src", applicationPic);
$(".appPicture").append(img);
}
Is there a way to run multiple functions with a single click event or a way to nest functions within a master without impacting the scope?
jquery function scope onclick this
add a comment |
up vote
0
down vote
favorite
I am currently making a dynamic webpage and trying to run multiple functions whenever the element with the class=application is clicked. The below code works but it's very repetitive.
$(document).on("click", ".application", renderImage);
$(document).on("click", ".application", renderText);
$(document).on("click", ".application", renderCodeButton);
$(document).on("click", ".application", renderAppButton);
I attempted to put these functions within one master function and run that with the .on() however as I use $(this).attr to pull information nesting the render functions within a master function ruins the scope.
Here is the renderImage function, though they are all very similar:
function renderImage () {
$(".appPicture").empty();
console.log(this);
let applicationPic = $(this).attr("data-image")
let img = $("<img>");
img.addClass("appPic")
img.attr("src", applicationPic);
$(".appPicture").append(img);
}
Is there a way to run multiple functions with a single click event or a way to nest functions within a master without impacting the scope?
jquery function scope onclick this
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am currently making a dynamic webpage and trying to run multiple functions whenever the element with the class=application is clicked. The below code works but it's very repetitive.
$(document).on("click", ".application", renderImage);
$(document).on("click", ".application", renderText);
$(document).on("click", ".application", renderCodeButton);
$(document).on("click", ".application", renderAppButton);
I attempted to put these functions within one master function and run that with the .on() however as I use $(this).attr to pull information nesting the render functions within a master function ruins the scope.
Here is the renderImage function, though they are all very similar:
function renderImage () {
$(".appPicture").empty();
console.log(this);
let applicationPic = $(this).attr("data-image")
let img = $("<img>");
img.addClass("appPic")
img.attr("src", applicationPic);
$(".appPicture").append(img);
}
Is there a way to run multiple functions with a single click event or a way to nest functions within a master without impacting the scope?
jquery function scope onclick this
I am currently making a dynamic webpage and trying to run multiple functions whenever the element with the class=application is clicked. The below code works but it's very repetitive.
$(document).on("click", ".application", renderImage);
$(document).on("click", ".application", renderText);
$(document).on("click", ".application", renderCodeButton);
$(document).on("click", ".application", renderAppButton);
I attempted to put these functions within one master function and run that with the .on() however as I use $(this).attr to pull information nesting the render functions within a master function ruins the scope.
Here is the renderImage function, though they are all very similar:
function renderImage () {
$(".appPicture").empty();
console.log(this);
let applicationPic = $(this).attr("data-image")
let img = $("<img>");
img.addClass("appPic")
img.attr("src", applicationPic);
$(".appPicture").append(img);
}
Is there a way to run multiple functions with a single click event or a way to nest functions within a master without impacting the scope?
jquery function scope onclick this
jquery function scope onclick this
edited Nov 5 at 4:13
Shiladitya
9,29391830
9,29391830
asked Nov 5 at 2:47
A. Andersen
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
$(document).on("click", ".application", function(){
let scope = this;
renderImage(scope);
renderText(scope);
renderCodeButton(scope);
renderAppButton(scope);
});
function renderImage (scope) {
$(".appPicture").empty();
console.log(scope);
let applicationPic = $(scope).attr("data-image")
let img = $("<img>");
img.addClass("appPic")
img.attr("src", applicationPic);
$(".appPicture").append(img);
}
you can do something like this
I've tried that but it changes the scope so $(this) is no longer the item I am clicking but is instead window.
– A. Andersen
Nov 5 at 3:05
i edit the answer, your function should have args that accept the scope you want..
– Darryl Ceguerra
Nov 5 at 3:29
Works perfect! Thank you.
– A. Andersen
Nov 6 at 0:54
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
$(document).on("click", ".application", function(){
let scope = this;
renderImage(scope);
renderText(scope);
renderCodeButton(scope);
renderAppButton(scope);
});
function renderImage (scope) {
$(".appPicture").empty();
console.log(scope);
let applicationPic = $(scope).attr("data-image")
let img = $("<img>");
img.addClass("appPic")
img.attr("src", applicationPic);
$(".appPicture").append(img);
}
you can do something like this
I've tried that but it changes the scope so $(this) is no longer the item I am clicking but is instead window.
– A. Andersen
Nov 5 at 3:05
i edit the answer, your function should have args that accept the scope you want..
– Darryl Ceguerra
Nov 5 at 3:29
Works perfect! Thank you.
– A. Andersen
Nov 6 at 0:54
add a comment |
up vote
0
down vote
accepted
$(document).on("click", ".application", function(){
let scope = this;
renderImage(scope);
renderText(scope);
renderCodeButton(scope);
renderAppButton(scope);
});
function renderImage (scope) {
$(".appPicture").empty();
console.log(scope);
let applicationPic = $(scope).attr("data-image")
let img = $("<img>");
img.addClass("appPic")
img.attr("src", applicationPic);
$(".appPicture").append(img);
}
you can do something like this
I've tried that but it changes the scope so $(this) is no longer the item I am clicking but is instead window.
– A. Andersen
Nov 5 at 3:05
i edit the answer, your function should have args that accept the scope you want..
– Darryl Ceguerra
Nov 5 at 3:29
Works perfect! Thank you.
– A. Andersen
Nov 6 at 0:54
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
$(document).on("click", ".application", function(){
let scope = this;
renderImage(scope);
renderText(scope);
renderCodeButton(scope);
renderAppButton(scope);
});
function renderImage (scope) {
$(".appPicture").empty();
console.log(scope);
let applicationPic = $(scope).attr("data-image")
let img = $("<img>");
img.addClass("appPic")
img.attr("src", applicationPic);
$(".appPicture").append(img);
}
you can do something like this
$(document).on("click", ".application", function(){
let scope = this;
renderImage(scope);
renderText(scope);
renderCodeButton(scope);
renderAppButton(scope);
});
function renderImage (scope) {
$(".appPicture").empty();
console.log(scope);
let applicationPic = $(scope).attr("data-image")
let img = $("<img>");
img.addClass("appPic")
img.attr("src", applicationPic);
$(".appPicture").append(img);
}
you can do something like this
edited Nov 5 at 3:27
answered Nov 5 at 2:57
Darryl Ceguerra
435
435
I've tried that but it changes the scope so $(this) is no longer the item I am clicking but is instead window.
– A. Andersen
Nov 5 at 3:05
i edit the answer, your function should have args that accept the scope you want..
– Darryl Ceguerra
Nov 5 at 3:29
Works perfect! Thank you.
– A. Andersen
Nov 6 at 0:54
add a comment |
I've tried that but it changes the scope so $(this) is no longer the item I am clicking but is instead window.
– A. Andersen
Nov 5 at 3:05
i edit the answer, your function should have args that accept the scope you want..
– Darryl Ceguerra
Nov 5 at 3:29
Works perfect! Thank you.
– A. Andersen
Nov 6 at 0:54
I've tried that but it changes the scope so $(this) is no longer the item I am clicking but is instead window.
– A. Andersen
Nov 5 at 3:05
I've tried that but it changes the scope so $(this) is no longer the item I am clicking but is instead window.
– A. Andersen
Nov 5 at 3:05
i edit the answer, your function should have args that accept the scope you want..
– Darryl Ceguerra
Nov 5 at 3:29
i edit the answer, your function should have args that accept the scope you want..
– Darryl Ceguerra
Nov 5 at 3:29
Works perfect! Thank you.
– A. Andersen
Nov 6 at 0:54
Works perfect! Thank you.
– A. Andersen
Nov 6 at 0:54
add a comment |
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147681%2fmultiple-functions-on-a-on-without-impacting-scope%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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