Accessing a class name that is stored in the Jquery variable












1














There are three pictures on the site.



<img src="images/kep1.jpg" class="kep1">
<img src="images/kep2.jpg" class="kep2">
<img src="images/kep4.jpg" class="kep4">


Click function to magnify one page at a time in the middle of the page page, but I'll store the necessary data in a variable.



var pos;
var t;
var h;
var pic;
$("img").click(function(){
pos = $(this).css("position");
pic = $(this).attr("class");
t = $(this).css("top");
h = $(this).css("height");
$(".szurkeDiv").show();
$(this).animate(
{
"position" : "fixed",
"top" : "50%",
"left" : "50%",
"margin-top" : "-300px",
"margin-left" : "-400px",
"width" : "800px",
"height" : "600px"
}, 1000, function(){}
);
});


Then I want you to click on the image in the middle of the page to animate it back to its original location. I would like to access the PIC variable but not succeed.



$(pic).animate(
{
"position" : pos,
"width" : "150px",
"margin-top" : "0px",
"margin-left" : "0px",
"left" : "5px",
"height" : h,
"top" : t
}, 1000, function(){}
);


How can I do this well, refer to the PIC variable?



Thanks for the help!!










share|improve this question
























  • Possible duplicate of jquery select elements by class using name from variable
    – Heretic Monkey
    Nov 12 '18 at 17:11










  • jQuery is not a language, it's a library for JavaScript. It doesn't do any magic, it's written in JavaScript code. You should learn the basics of JavaScript before trying to learn how to use any third-party library. I'm not trying to sound judgmental here, just save yourself some trouble and sit through a proper lesson.
    – Jacque Goupil
    Nov 12 '18 at 17:13












  • I'm just learning this. This was the home lesson! :-)
    – PengeDroid
    Nov 12 '18 at 17:40
















1














There are three pictures on the site.



<img src="images/kep1.jpg" class="kep1">
<img src="images/kep2.jpg" class="kep2">
<img src="images/kep4.jpg" class="kep4">


Click function to magnify one page at a time in the middle of the page page, but I'll store the necessary data in a variable.



var pos;
var t;
var h;
var pic;
$("img").click(function(){
pos = $(this).css("position");
pic = $(this).attr("class");
t = $(this).css("top");
h = $(this).css("height");
$(".szurkeDiv").show();
$(this).animate(
{
"position" : "fixed",
"top" : "50%",
"left" : "50%",
"margin-top" : "-300px",
"margin-left" : "-400px",
"width" : "800px",
"height" : "600px"
}, 1000, function(){}
);
});


Then I want you to click on the image in the middle of the page to animate it back to its original location. I would like to access the PIC variable but not succeed.



$(pic).animate(
{
"position" : pos,
"width" : "150px",
"margin-top" : "0px",
"margin-left" : "0px",
"left" : "5px",
"height" : h,
"top" : t
}, 1000, function(){}
);


How can I do this well, refer to the PIC variable?



Thanks for the help!!










share|improve this question
























  • Possible duplicate of jquery select elements by class using name from variable
    – Heretic Monkey
    Nov 12 '18 at 17:11










  • jQuery is not a language, it's a library for JavaScript. It doesn't do any magic, it's written in JavaScript code. You should learn the basics of JavaScript before trying to learn how to use any third-party library. I'm not trying to sound judgmental here, just save yourself some trouble and sit through a proper lesson.
    – Jacque Goupil
    Nov 12 '18 at 17:13












  • I'm just learning this. This was the home lesson! :-)
    – PengeDroid
    Nov 12 '18 at 17:40














1












1








1







There are three pictures on the site.



<img src="images/kep1.jpg" class="kep1">
<img src="images/kep2.jpg" class="kep2">
<img src="images/kep4.jpg" class="kep4">


Click function to magnify one page at a time in the middle of the page page, but I'll store the necessary data in a variable.



var pos;
var t;
var h;
var pic;
$("img").click(function(){
pos = $(this).css("position");
pic = $(this).attr("class");
t = $(this).css("top");
h = $(this).css("height");
$(".szurkeDiv").show();
$(this).animate(
{
"position" : "fixed",
"top" : "50%",
"left" : "50%",
"margin-top" : "-300px",
"margin-left" : "-400px",
"width" : "800px",
"height" : "600px"
}, 1000, function(){}
);
});


Then I want you to click on the image in the middle of the page to animate it back to its original location. I would like to access the PIC variable but not succeed.



$(pic).animate(
{
"position" : pos,
"width" : "150px",
"margin-top" : "0px",
"margin-left" : "0px",
"left" : "5px",
"height" : h,
"top" : t
}, 1000, function(){}
);


How can I do this well, refer to the PIC variable?



Thanks for the help!!










share|improve this question















There are three pictures on the site.



<img src="images/kep1.jpg" class="kep1">
<img src="images/kep2.jpg" class="kep2">
<img src="images/kep4.jpg" class="kep4">


Click function to magnify one page at a time in the middle of the page page, but I'll store the necessary data in a variable.



var pos;
var t;
var h;
var pic;
$("img").click(function(){
pos = $(this).css("position");
pic = $(this).attr("class");
t = $(this).css("top");
h = $(this).css("height");
$(".szurkeDiv").show();
$(this).animate(
{
"position" : "fixed",
"top" : "50%",
"left" : "50%",
"margin-top" : "-300px",
"margin-left" : "-400px",
"width" : "800px",
"height" : "600px"
}, 1000, function(){}
);
});


Then I want you to click on the image in the middle of the page to animate it back to its original location. I would like to access the PIC variable but not succeed.



$(pic).animate(
{
"position" : pos,
"width" : "150px",
"margin-top" : "0px",
"margin-left" : "0px",
"left" : "5px",
"height" : h,
"top" : t
}, 1000, function(){}
);


How can I do this well, refer to the PIC variable?



Thanks for the help!!







jquery html css






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 16:58

























asked Nov 12 '18 at 16:52









PengeDroid

135




135












  • Possible duplicate of jquery select elements by class using name from variable
    – Heretic Monkey
    Nov 12 '18 at 17:11










  • jQuery is not a language, it's a library for JavaScript. It doesn't do any magic, it's written in JavaScript code. You should learn the basics of JavaScript before trying to learn how to use any third-party library. I'm not trying to sound judgmental here, just save yourself some trouble and sit through a proper lesson.
    – Jacque Goupil
    Nov 12 '18 at 17:13












  • I'm just learning this. This was the home lesson! :-)
    – PengeDroid
    Nov 12 '18 at 17:40


















  • Possible duplicate of jquery select elements by class using name from variable
    – Heretic Monkey
    Nov 12 '18 at 17:11










  • jQuery is not a language, it's a library for JavaScript. It doesn't do any magic, it's written in JavaScript code. You should learn the basics of JavaScript before trying to learn how to use any third-party library. I'm not trying to sound judgmental here, just save yourself some trouble and sit through a proper lesson.
    – Jacque Goupil
    Nov 12 '18 at 17:13












  • I'm just learning this. This was the home lesson! :-)
    – PengeDroid
    Nov 12 '18 at 17:40
















Possible duplicate of jquery select elements by class using name from variable
– Heretic Monkey
Nov 12 '18 at 17:11




Possible duplicate of jquery select elements by class using name from variable
– Heretic Monkey
Nov 12 '18 at 17:11












jQuery is not a language, it's a library for JavaScript. It doesn't do any magic, it's written in JavaScript code. You should learn the basics of JavaScript before trying to learn how to use any third-party library. I'm not trying to sound judgmental here, just save yourself some trouble and sit through a proper lesson.
– Jacque Goupil
Nov 12 '18 at 17:13






jQuery is not a language, it's a library for JavaScript. It doesn't do any magic, it's written in JavaScript code. You should learn the basics of JavaScript before trying to learn how to use any third-party library. I'm not trying to sound judgmental here, just save yourself some trouble and sit through a proper lesson.
– Jacque Goupil
Nov 12 '18 at 17:13














I'm just learning this. This was the home lesson! :-)
– PengeDroid
Nov 12 '18 at 17:40




I'm just learning this. This was the home lesson! :-)
– PengeDroid
Nov 12 '18 at 17:40












2 Answers
2






active

oldest

votes


















2














try it with $('.' + pic) instead of $(pic).



See https://jsfiddle.net/g15v3c4z/



var pic = $('p').attr("class");

$('.' + pic).html('world!');





share|improve this answer





















  • Thanks!!! :-)))
    – PengeDroid
    Nov 12 '18 at 17:44



















0














Use plain CSS for the animation:






var img = $("img")

img.on("click", function(){
$(this).toggleClass("zoom");
})

img {
transition: all 0.5s ease;

}

.zoom {
transform:scale(2);
transform-origin: 0 0;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<img src="https://via.placeholder.com/350" alt="">
<img src="https://via.placeholder.com/350" alt="">

<img src="https://via.placeholder.com/350" alt="">

</div>








share|improve this answer





















    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266706%2faccessing-a-class-name-that-is-stored-in-the-jquery-variable%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    try it with $('.' + pic) instead of $(pic).



    See https://jsfiddle.net/g15v3c4z/



    var pic = $('p').attr("class");

    $('.' + pic).html('world!');





    share|improve this answer





















    • Thanks!!! :-)))
      – PengeDroid
      Nov 12 '18 at 17:44
















    2














    try it with $('.' + pic) instead of $(pic).



    See https://jsfiddle.net/g15v3c4z/



    var pic = $('p').attr("class");

    $('.' + pic).html('world!');





    share|improve this answer





















    • Thanks!!! :-)))
      – PengeDroid
      Nov 12 '18 at 17:44














    2












    2








    2






    try it with $('.' + pic) instead of $(pic).



    See https://jsfiddle.net/g15v3c4z/



    var pic = $('p').attr("class");

    $('.' + pic).html('world!');





    share|improve this answer












    try it with $('.' + pic) instead of $(pic).



    See https://jsfiddle.net/g15v3c4z/



    var pic = $('p').attr("class");

    $('.' + pic).html('world!');






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 '18 at 17:02









    Andreas

    443




    443












    • Thanks!!! :-)))
      – PengeDroid
      Nov 12 '18 at 17:44


















    • Thanks!!! :-)))
      – PengeDroid
      Nov 12 '18 at 17:44
















    Thanks!!! :-)))
    – PengeDroid
    Nov 12 '18 at 17:44




    Thanks!!! :-)))
    – PengeDroid
    Nov 12 '18 at 17:44













    0














    Use plain CSS for the animation:






    var img = $("img")

    img.on("click", function(){
    $(this).toggleClass("zoom");
    })

    img {
    transition: all 0.5s ease;

    }

    .zoom {
    transform:scale(2);
    transform-origin: 0 0;
    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="container">
    <img src="https://via.placeholder.com/350" alt="">
    <img src="https://via.placeholder.com/350" alt="">

    <img src="https://via.placeholder.com/350" alt="">

    </div>








    share|improve this answer


























      0














      Use plain CSS for the animation:






      var img = $("img")

      img.on("click", function(){
      $(this).toggleClass("zoom");
      })

      img {
      transition: all 0.5s ease;

      }

      .zoom {
      transform:scale(2);
      transform-origin: 0 0;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="container">
      <img src="https://via.placeholder.com/350" alt="">
      <img src="https://via.placeholder.com/350" alt="">

      <img src="https://via.placeholder.com/350" alt="">

      </div>








      share|improve this answer
























        0












        0








        0






        Use plain CSS for the animation:






        var img = $("img")

        img.on("click", function(){
        $(this).toggleClass("zoom");
        })

        img {
        transition: all 0.5s ease;

        }

        .zoom {
        transform:scale(2);
        transform-origin: 0 0;
        }

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <div class="container">
        <img src="https://via.placeholder.com/350" alt="">
        <img src="https://via.placeholder.com/350" alt="">

        <img src="https://via.placeholder.com/350" alt="">

        </div>








        share|improve this answer












        Use plain CSS for the animation:






        var img = $("img")

        img.on("click", function(){
        $(this).toggleClass("zoom");
        })

        img {
        transition: all 0.5s ease;

        }

        .zoom {
        transform:scale(2);
        transform-origin: 0 0;
        }

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <div class="container">
        <img src="https://via.placeholder.com/350" alt="">
        <img src="https://via.placeholder.com/350" alt="">

        <img src="https://via.placeholder.com/350" alt="">

        </div>








        var img = $("img")

        img.on("click", function(){
        $(this).toggleClass("zoom");
        })

        img {
        transition: all 0.5s ease;

        }

        .zoom {
        transform:scale(2);
        transform-origin: 0 0;
        }

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <div class="container">
        <img src="https://via.placeholder.com/350" alt="">
        <img src="https://via.placeholder.com/350" alt="">

        <img src="https://via.placeholder.com/350" alt="">

        </div>





        var img = $("img")

        img.on("click", function(){
        $(this).toggleClass("zoom");
        })

        img {
        transition: all 0.5s ease;

        }

        .zoom {
        transform:scale(2);
        transform-origin: 0 0;
        }

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <div class="container">
        <img src="https://via.placeholder.com/350" alt="">
        <img src="https://via.placeholder.com/350" alt="">

        <img src="https://via.placeholder.com/350" alt="">

        </div>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 17:24









        Ihsan S

        324




        324






























            draft saved

            draft discarded




















































            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266706%2faccessing-a-class-name-that-is-stored-in-the-jquery-variable%23new-answer', 'question_page');
            }
            );

            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







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud