document.getElementById(“id”).value , change id by adding a variable to it












0














function totalCost(obj,a){    
var price = document.getElementById("price").value;
}


I want to create a new id adding variable a. If a =1, then the next id will be price1. How can I do it?










share|improve this question




















  • 2




    "price" + a ?
    – Peter B
    Nov 12 '18 at 13:03










  • setAttribute?
    – str
    Nov 12 '18 at 13:04






  • 1




    My apologies, could you be more precise about what you are trying to achieve? What is your goal?
    – Tornike Shavishvili
    Nov 12 '18 at 13:05












  • "price" + a works fine . Thanks @Peter
    – Shakir Uz Zaman
    Nov 12 '18 at 14:00
















0














function totalCost(obj,a){    
var price = document.getElementById("price").value;
}


I want to create a new id adding variable a. If a =1, then the next id will be price1. How can I do it?










share|improve this question




















  • 2




    "price" + a ?
    – Peter B
    Nov 12 '18 at 13:03










  • setAttribute?
    – str
    Nov 12 '18 at 13:04






  • 1




    My apologies, could you be more precise about what you are trying to achieve? What is your goal?
    – Tornike Shavishvili
    Nov 12 '18 at 13:05












  • "price" + a works fine . Thanks @Peter
    – Shakir Uz Zaman
    Nov 12 '18 at 14:00














0












0








0







function totalCost(obj,a){    
var price = document.getElementById("price").value;
}


I want to create a new id adding variable a. If a =1, then the next id will be price1. How can I do it?










share|improve this question















function totalCost(obj,a){    
var price = document.getElementById("price").value;
}


I want to create a new id adding variable a. If a =1, then the next id will be price1. How can I do it?







javascript html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 14:04









Mamun

25.6k71428




25.6k71428










asked Nov 12 '18 at 13:02









Shakir Uz ZamanShakir Uz Zaman

426




426








  • 2




    "price" + a ?
    – Peter B
    Nov 12 '18 at 13:03










  • setAttribute?
    – str
    Nov 12 '18 at 13:04






  • 1




    My apologies, could you be more precise about what you are trying to achieve? What is your goal?
    – Tornike Shavishvili
    Nov 12 '18 at 13:05












  • "price" + a works fine . Thanks @Peter
    – Shakir Uz Zaman
    Nov 12 '18 at 14:00














  • 2




    "price" + a ?
    – Peter B
    Nov 12 '18 at 13:03










  • setAttribute?
    – str
    Nov 12 '18 at 13:04






  • 1




    My apologies, could you be more precise about what you are trying to achieve? What is your goal?
    – Tornike Shavishvili
    Nov 12 '18 at 13:05












  • "price" + a works fine . Thanks @Peter
    – Shakir Uz Zaman
    Nov 12 '18 at 14:00








2




2




"price" + a ?
– Peter B
Nov 12 '18 at 13:03




"price" + a ?
– Peter B
Nov 12 '18 at 13:03












setAttribute?
– str
Nov 12 '18 at 13:04




setAttribute?
– str
Nov 12 '18 at 13:04




1




1




My apologies, could you be more precise about what you are trying to achieve? What is your goal?
– Tornike Shavishvili
Nov 12 '18 at 13:05






My apologies, could you be more precise about what you are trying to achieve? What is your goal?
– Tornike Shavishvili
Nov 12 '18 at 13:05














"price" + a works fine . Thanks @Peter
– Shakir Uz Zaman
Nov 12 '18 at 14:00




"price" + a works fine . Thanks @Peter
– Shakir Uz Zaman
Nov 12 '18 at 14:00












2 Answers
2






active

oldest

votes


















1














You can use Template Literals which allows embedded expressions:



function totalCost(obj,a){    
var price = document.getElementById(`price${a}`).value;
}


OR: Using String Concatenation (if browser is yet to support Template Literals)



function totalCost(obj,a){    
var price = document.getElementById('price' + a).value;
}





share|improve this answer























  • Template literals is an ES6 feature and are still not supported by all the browsers(IE11[caniuse.com/#feat=template-literals]). So always suggest them as an alternative syntax.
    – jay surya
    Nov 12 '18 at 13:19










  • @ShakirUzZaman, you can post comment if the answer does not solve your problem...
    – Mamun
    Nov 12 '18 at 13:48










  • ("price"+a ) is working fine . Thanks @Mamun
    – Shakir Uz Zaman
    Nov 12 '18 at 13:58










  • @ShakirUzZaman, you are most welcome....thanks for letting me know:)
    – Mamun
    Nov 12 '18 at 14:00



















0














function totalCost(obj,a)
{
var price = document.getElementById("price"+a).value;
}


this solution is working fine .






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%2f53262757%2fdocument-getelementbyidid-value-change-id-by-adding-a-variable-to-it%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









    1














    You can use Template Literals which allows embedded expressions:



    function totalCost(obj,a){    
    var price = document.getElementById(`price${a}`).value;
    }


    OR: Using String Concatenation (if browser is yet to support Template Literals)



    function totalCost(obj,a){    
    var price = document.getElementById('price' + a).value;
    }





    share|improve this answer























    • Template literals is an ES6 feature and are still not supported by all the browsers(IE11[caniuse.com/#feat=template-literals]). So always suggest them as an alternative syntax.
      – jay surya
      Nov 12 '18 at 13:19










    • @ShakirUzZaman, you can post comment if the answer does not solve your problem...
      – Mamun
      Nov 12 '18 at 13:48










    • ("price"+a ) is working fine . Thanks @Mamun
      – Shakir Uz Zaman
      Nov 12 '18 at 13:58










    • @ShakirUzZaman, you are most welcome....thanks for letting me know:)
      – Mamun
      Nov 12 '18 at 14:00
















    1














    You can use Template Literals which allows embedded expressions:



    function totalCost(obj,a){    
    var price = document.getElementById(`price${a}`).value;
    }


    OR: Using String Concatenation (if browser is yet to support Template Literals)



    function totalCost(obj,a){    
    var price = document.getElementById('price' + a).value;
    }





    share|improve this answer























    • Template literals is an ES6 feature and are still not supported by all the browsers(IE11[caniuse.com/#feat=template-literals]). So always suggest them as an alternative syntax.
      – jay surya
      Nov 12 '18 at 13:19










    • @ShakirUzZaman, you can post comment if the answer does not solve your problem...
      – Mamun
      Nov 12 '18 at 13:48










    • ("price"+a ) is working fine . Thanks @Mamun
      – Shakir Uz Zaman
      Nov 12 '18 at 13:58










    • @ShakirUzZaman, you are most welcome....thanks for letting me know:)
      – Mamun
      Nov 12 '18 at 14:00














    1












    1








    1






    You can use Template Literals which allows embedded expressions:



    function totalCost(obj,a){    
    var price = document.getElementById(`price${a}`).value;
    }


    OR: Using String Concatenation (if browser is yet to support Template Literals)



    function totalCost(obj,a){    
    var price = document.getElementById('price' + a).value;
    }





    share|improve this answer














    You can use Template Literals which allows embedded expressions:



    function totalCost(obj,a){    
    var price = document.getElementById(`price${a}`).value;
    }


    OR: Using String Concatenation (if browser is yet to support Template Literals)



    function totalCost(obj,a){    
    var price = document.getElementById('price' + a).value;
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 '18 at 13:25

























    answered Nov 12 '18 at 13:07









    MamunMamun

    25.6k71428




    25.6k71428












    • Template literals is an ES6 feature and are still not supported by all the browsers(IE11[caniuse.com/#feat=template-literals]). So always suggest them as an alternative syntax.
      – jay surya
      Nov 12 '18 at 13:19










    • @ShakirUzZaman, you can post comment if the answer does not solve your problem...
      – Mamun
      Nov 12 '18 at 13:48










    • ("price"+a ) is working fine . Thanks @Mamun
      – Shakir Uz Zaman
      Nov 12 '18 at 13:58










    • @ShakirUzZaman, you are most welcome....thanks for letting me know:)
      – Mamun
      Nov 12 '18 at 14:00


















    • Template literals is an ES6 feature and are still not supported by all the browsers(IE11[caniuse.com/#feat=template-literals]). So always suggest them as an alternative syntax.
      – jay surya
      Nov 12 '18 at 13:19










    • @ShakirUzZaman, you can post comment if the answer does not solve your problem...
      – Mamun
      Nov 12 '18 at 13:48










    • ("price"+a ) is working fine . Thanks @Mamun
      – Shakir Uz Zaman
      Nov 12 '18 at 13:58










    • @ShakirUzZaman, you are most welcome....thanks for letting me know:)
      – Mamun
      Nov 12 '18 at 14:00
















    Template literals is an ES6 feature and are still not supported by all the browsers(IE11[caniuse.com/#feat=template-literals]). So always suggest them as an alternative syntax.
    – jay surya
    Nov 12 '18 at 13:19




    Template literals is an ES6 feature and are still not supported by all the browsers(IE11[caniuse.com/#feat=template-literals]). So always suggest them as an alternative syntax.
    – jay surya
    Nov 12 '18 at 13:19












    @ShakirUzZaman, you can post comment if the answer does not solve your problem...
    – Mamun
    Nov 12 '18 at 13:48




    @ShakirUzZaman, you can post comment if the answer does not solve your problem...
    – Mamun
    Nov 12 '18 at 13:48












    ("price"+a ) is working fine . Thanks @Mamun
    – Shakir Uz Zaman
    Nov 12 '18 at 13:58




    ("price"+a ) is working fine . Thanks @Mamun
    – Shakir Uz Zaman
    Nov 12 '18 at 13:58












    @ShakirUzZaman, you are most welcome....thanks for letting me know:)
    – Mamun
    Nov 12 '18 at 14:00




    @ShakirUzZaman, you are most welcome....thanks for letting me know:)
    – Mamun
    Nov 12 '18 at 14:00













    0














    function totalCost(obj,a)
    {
    var price = document.getElementById("price"+a).value;
    }


    this solution is working fine .






    share|improve this answer


























      0














      function totalCost(obj,a)
      {
      var price = document.getElementById("price"+a).value;
      }


      this solution is working fine .






      share|improve this answer
























        0












        0








        0






        function totalCost(obj,a)
        {
        var price = document.getElementById("price"+a).value;
        }


        this solution is working fine .






        share|improve this answer












        function totalCost(obj,a)
        {
        var price = document.getElementById("price"+a).value;
        }


        this solution is working fine .







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 13:57









        Shakir Uz ZamanShakir Uz Zaman

        426




        426






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262757%2fdocument-getelementbyidid-value-change-id-by-adding-a-variable-to-it%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







            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ