How to dynamically change var value on click?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















Actually in my site i have a table with a lot of download link for each version so i would be able to change the file version that the user is trying to download.



Like here is one of items from my table:



<tr>
<td> App</td>
<td id="0.9.0">0.9.0</td>
<td>02/01/2019</td>
<td><a href="#" aria-disabled="true" data-toggle="modal" data-target="#exampleModalCenter" onclick="version = document.getElementById("0.9.0").innerText; alert.version;>Download</a></td>
</tr>


After i press download i'd open the bootstrap modal in which i should insert the right password and then i the user press download it should download the right file version.



Here is my method used in onClick of the modal button:



<script>
var input = document.getElementById("Key");
var btn = document.getElementById("download");
var version;

btn.addEventListener("click", function () {
if (input.value == "1234") {
window.location = '/App/App_0.9.0.apk'; (this 0.9.0 should be dynamic)
$("#exampleModalCenter").modal('hide')
}
});
</script>


UPDATE AFTER (Oen44 answer):



I've changed the a tag to this:



<tr>
<td>App</td>
<td>0.9.0</td>
<td>02/01/2019</td>
<td><a href="#" aria-disabled="true" data-toggle="modal" data-target="#exampleModalCenter" data-version="0.9.0" onclick="version = this.getAttribute('data-version');">Download</a></td>
</tr>
<tr>
<td>App</td>
<td>0.2.3</td>
<td>02/01/2019</td>
<td><a href="#" aria-disabled="true" data-toggle="modal" data-target="#exampleModalCenter" data-version="0.2.3" onclick="version = this.getAttribute('data-version');">Download</a></td>
</tr>


While the script is still the same :



var input = document.getElementById("Key");
var btn = document.getElementById("download");
let version;

btn.addEventListener("click", function () {
if (input.value == "1234") {
window.location = '/App/App_${version}.apk';
$("#exampleModalCenter").modal('hide')
}
});


But when i'm trying to download the file but nothing is heppening










share|improve this question

























  • Can you use dots as id? what will prevent #0.9.0 to be interpolated as an element with id 0 and with class 9 and class 0

    – Alon Eitan
    Jan 4 at 16:51











  • @AlonEitan so how could i do as i could have 30 items in the table, i have to put different id for each <td> version with not dotted names?

    – JohnKarry
    Jan 4 at 16:54






  • 1





    id="0-9-0" IMO

    – Alon Eitan
    Jan 4 at 16:55











  • But that is not efficient.

    – Oen44
    Jan 4 at 16:55






  • 1





    @AlonEitan Dots are valid and getElementById will work without problems. It's just weird looking.

    – Oen44
    Jan 4 at 17:06


















2















Actually in my site i have a table with a lot of download link for each version so i would be able to change the file version that the user is trying to download.



Like here is one of items from my table:



<tr>
<td> App</td>
<td id="0.9.0">0.9.0</td>
<td>02/01/2019</td>
<td><a href="#" aria-disabled="true" data-toggle="modal" data-target="#exampleModalCenter" onclick="version = document.getElementById("0.9.0").innerText; alert.version;>Download</a></td>
</tr>


After i press download i'd open the bootstrap modal in which i should insert the right password and then i the user press download it should download the right file version.



Here is my method used in onClick of the modal button:



<script>
var input = document.getElementById("Key");
var btn = document.getElementById("download");
var version;

btn.addEventListener("click", function () {
if (input.value == "1234") {
window.location = '/App/App_0.9.0.apk'; (this 0.9.0 should be dynamic)
$("#exampleModalCenter").modal('hide')
}
});
</script>


UPDATE AFTER (Oen44 answer):



I've changed the a tag to this:



<tr>
<td>App</td>
<td>0.9.0</td>
<td>02/01/2019</td>
<td><a href="#" aria-disabled="true" data-toggle="modal" data-target="#exampleModalCenter" data-version="0.9.0" onclick="version = this.getAttribute('data-version');">Download</a></td>
</tr>
<tr>
<td>App</td>
<td>0.2.3</td>
<td>02/01/2019</td>
<td><a href="#" aria-disabled="true" data-toggle="modal" data-target="#exampleModalCenter" data-version="0.2.3" onclick="version = this.getAttribute('data-version');">Download</a></td>
</tr>


While the script is still the same :



var input = document.getElementById("Key");
var btn = document.getElementById("download");
let version;

btn.addEventListener("click", function () {
if (input.value == "1234") {
window.location = '/App/App_${version}.apk';
$("#exampleModalCenter").modal('hide')
}
});


But when i'm trying to download the file but nothing is heppening










share|improve this question

























  • Can you use dots as id? what will prevent #0.9.0 to be interpolated as an element with id 0 and with class 9 and class 0

    – Alon Eitan
    Jan 4 at 16:51











  • @AlonEitan so how could i do as i could have 30 items in the table, i have to put different id for each <td> version with not dotted names?

    – JohnKarry
    Jan 4 at 16:54






  • 1





    id="0-9-0" IMO

    – Alon Eitan
    Jan 4 at 16:55











  • But that is not efficient.

    – Oen44
    Jan 4 at 16:55






  • 1





    @AlonEitan Dots are valid and getElementById will work without problems. It's just weird looking.

    – Oen44
    Jan 4 at 17:06














2












2








2








Actually in my site i have a table with a lot of download link for each version so i would be able to change the file version that the user is trying to download.



Like here is one of items from my table:



<tr>
<td> App</td>
<td id="0.9.0">0.9.0</td>
<td>02/01/2019</td>
<td><a href="#" aria-disabled="true" data-toggle="modal" data-target="#exampleModalCenter" onclick="version = document.getElementById("0.9.0").innerText; alert.version;>Download</a></td>
</tr>


After i press download i'd open the bootstrap modal in which i should insert the right password and then i the user press download it should download the right file version.



Here is my method used in onClick of the modal button:



<script>
var input = document.getElementById("Key");
var btn = document.getElementById("download");
var version;

btn.addEventListener("click", function () {
if (input.value == "1234") {
window.location = '/App/App_0.9.0.apk'; (this 0.9.0 should be dynamic)
$("#exampleModalCenter").modal('hide')
}
});
</script>


UPDATE AFTER (Oen44 answer):



I've changed the a tag to this:



<tr>
<td>App</td>
<td>0.9.0</td>
<td>02/01/2019</td>
<td><a href="#" aria-disabled="true" data-toggle="modal" data-target="#exampleModalCenter" data-version="0.9.0" onclick="version = this.getAttribute('data-version');">Download</a></td>
</tr>
<tr>
<td>App</td>
<td>0.2.3</td>
<td>02/01/2019</td>
<td><a href="#" aria-disabled="true" data-toggle="modal" data-target="#exampleModalCenter" data-version="0.2.3" onclick="version = this.getAttribute('data-version');">Download</a></td>
</tr>


While the script is still the same :



var input = document.getElementById("Key");
var btn = document.getElementById("download");
let version;

btn.addEventListener("click", function () {
if (input.value == "1234") {
window.location = '/App/App_${version}.apk';
$("#exampleModalCenter").modal('hide')
}
});


But when i'm trying to download the file but nothing is heppening










share|improve this question
















Actually in my site i have a table with a lot of download link for each version so i would be able to change the file version that the user is trying to download.



Like here is one of items from my table:



<tr>
<td> App</td>
<td id="0.9.0">0.9.0</td>
<td>02/01/2019</td>
<td><a href="#" aria-disabled="true" data-toggle="modal" data-target="#exampleModalCenter" onclick="version = document.getElementById("0.9.0").innerText; alert.version;>Download</a></td>
</tr>


After i press download i'd open the bootstrap modal in which i should insert the right password and then i the user press download it should download the right file version.



Here is my method used in onClick of the modal button:



<script>
var input = document.getElementById("Key");
var btn = document.getElementById("download");
var version;

btn.addEventListener("click", function () {
if (input.value == "1234") {
window.location = '/App/App_0.9.0.apk'; (this 0.9.0 should be dynamic)
$("#exampleModalCenter").modal('hide')
}
});
</script>


UPDATE AFTER (Oen44 answer):



I've changed the a tag to this:



<tr>
<td>App</td>
<td>0.9.0</td>
<td>02/01/2019</td>
<td><a href="#" aria-disabled="true" data-toggle="modal" data-target="#exampleModalCenter" data-version="0.9.0" onclick="version = this.getAttribute('data-version');">Download</a></td>
</tr>
<tr>
<td>App</td>
<td>0.2.3</td>
<td>02/01/2019</td>
<td><a href="#" aria-disabled="true" data-toggle="modal" data-target="#exampleModalCenter" data-version="0.2.3" onclick="version = this.getAttribute('data-version');">Download</a></td>
</tr>


While the script is still the same :



var input = document.getElementById("Key");
var btn = document.getElementById("download");
let version;

btn.addEventListener("click", function () {
if (input.value == "1234") {
window.location = '/App/App_${version}.apk';
$("#exampleModalCenter").modal('hide')
}
});


But when i'm trying to download the file but nothing is heppening







javascript html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 17:53









Oen44

1,8291919




1,8291919










asked Jan 4 at 16:46









JohnKarryJohnKarry

201117




201117













  • Can you use dots as id? what will prevent #0.9.0 to be interpolated as an element with id 0 and with class 9 and class 0

    – Alon Eitan
    Jan 4 at 16:51











  • @AlonEitan so how could i do as i could have 30 items in the table, i have to put different id for each <td> version with not dotted names?

    – JohnKarry
    Jan 4 at 16:54






  • 1





    id="0-9-0" IMO

    – Alon Eitan
    Jan 4 at 16:55











  • But that is not efficient.

    – Oen44
    Jan 4 at 16:55






  • 1





    @AlonEitan Dots are valid and getElementById will work without problems. It's just weird looking.

    – Oen44
    Jan 4 at 17:06



















  • Can you use dots as id? what will prevent #0.9.0 to be interpolated as an element with id 0 and with class 9 and class 0

    – Alon Eitan
    Jan 4 at 16:51











  • @AlonEitan so how could i do as i could have 30 items in the table, i have to put different id for each <td> version with not dotted names?

    – JohnKarry
    Jan 4 at 16:54






  • 1





    id="0-9-0" IMO

    – Alon Eitan
    Jan 4 at 16:55











  • But that is not efficient.

    – Oen44
    Jan 4 at 16:55






  • 1





    @AlonEitan Dots are valid and getElementById will work without problems. It's just weird looking.

    – Oen44
    Jan 4 at 17:06

















Can you use dots as id? what will prevent #0.9.0 to be interpolated as an element with id 0 and with class 9 and class 0

– Alon Eitan
Jan 4 at 16:51





Can you use dots as id? what will prevent #0.9.0 to be interpolated as an element with id 0 and with class 9 and class 0

– Alon Eitan
Jan 4 at 16:51













@AlonEitan so how could i do as i could have 30 items in the table, i have to put different id for each <td> version with not dotted names?

– JohnKarry
Jan 4 at 16:54





@AlonEitan so how could i do as i could have 30 items in the table, i have to put different id for each <td> version with not dotted names?

– JohnKarry
Jan 4 at 16:54




1




1





id="0-9-0" IMO

– Alon Eitan
Jan 4 at 16:55





id="0-9-0" IMO

– Alon Eitan
Jan 4 at 16:55













But that is not efficient.

– Oen44
Jan 4 at 16:55





But that is not efficient.

– Oen44
Jan 4 at 16:55




1




1





@AlonEitan Dots are valid and getElementById will work without problems. It's just weird looking.

– Oen44
Jan 4 at 17:06





@AlonEitan Dots are valid and getElementById will work without problems. It's just weird looking.

– Oen44
Jan 4 at 17:06












1 Answer
1






active

oldest

votes


















3














Two things to change



<td id="0.9.0">0.9.0</td> - don't use that format as id.



<td id="app-version">0.9.0</td> - way better



let version = document.getElementById('app-version').innerText;
window.location = `/App/App_${version}.apk`; // now with generate /App/App_0.9.0.apk


Edit



<a href="#" data-version="0.9.0" onclick="version = this.getAttribute('data-version');">Download</a>


Edit2



If you want to format string in JS using ${} then string must be in ``



`/App/RealcoApp_${version}.apk`





share|improve this answer


























  • should i put "let version = document.getElementById('app-version').innerText;" in the onClick? Actually i have to change the id for each td or i doesnot? as i have a lot of td with the different versions

    – JohnKarry
    Jan 4 at 16:53






  • 1





    id must be unique. If you are using button then you can use data attribute on that button and set version number there.

    – Oen44
    Jan 4 at 16:56






  • 1





    Made edit, should help.

    – Oen44
    Jan 4 at 17:01






  • 1





    just solved by using ' /App/RealcoApp_' + version +'.apk' ' thank you :)

    – JohnKarry
    Jan 4 at 17:16






  • 1





    That will work too, good job.

    – Oen44
    Jan 4 at 17:17












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%2f54042992%2fhow-to-dynamically-change-var-value-on-click%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









3














Two things to change



<td id="0.9.0">0.9.0</td> - don't use that format as id.



<td id="app-version">0.9.0</td> - way better



let version = document.getElementById('app-version').innerText;
window.location = `/App/App_${version}.apk`; // now with generate /App/App_0.9.0.apk


Edit



<a href="#" data-version="0.9.0" onclick="version = this.getAttribute('data-version');">Download</a>


Edit2



If you want to format string in JS using ${} then string must be in ``



`/App/RealcoApp_${version}.apk`





share|improve this answer


























  • should i put "let version = document.getElementById('app-version').innerText;" in the onClick? Actually i have to change the id for each td or i doesnot? as i have a lot of td with the different versions

    – JohnKarry
    Jan 4 at 16:53






  • 1





    id must be unique. If you are using button then you can use data attribute on that button and set version number there.

    – Oen44
    Jan 4 at 16:56






  • 1





    Made edit, should help.

    – Oen44
    Jan 4 at 17:01






  • 1





    just solved by using ' /App/RealcoApp_' + version +'.apk' ' thank you :)

    – JohnKarry
    Jan 4 at 17:16






  • 1





    That will work too, good job.

    – Oen44
    Jan 4 at 17:17
















3














Two things to change



<td id="0.9.0">0.9.0</td> - don't use that format as id.



<td id="app-version">0.9.0</td> - way better



let version = document.getElementById('app-version').innerText;
window.location = `/App/App_${version}.apk`; // now with generate /App/App_0.9.0.apk


Edit



<a href="#" data-version="0.9.0" onclick="version = this.getAttribute('data-version');">Download</a>


Edit2



If you want to format string in JS using ${} then string must be in ``



`/App/RealcoApp_${version}.apk`





share|improve this answer


























  • should i put "let version = document.getElementById('app-version').innerText;" in the onClick? Actually i have to change the id for each td or i doesnot? as i have a lot of td with the different versions

    – JohnKarry
    Jan 4 at 16:53






  • 1





    id must be unique. If you are using button then you can use data attribute on that button and set version number there.

    – Oen44
    Jan 4 at 16:56






  • 1





    Made edit, should help.

    – Oen44
    Jan 4 at 17:01






  • 1





    just solved by using ' /App/RealcoApp_' + version +'.apk' ' thank you :)

    – JohnKarry
    Jan 4 at 17:16






  • 1





    That will work too, good job.

    – Oen44
    Jan 4 at 17:17














3












3








3







Two things to change



<td id="0.9.0">0.9.0</td> - don't use that format as id.



<td id="app-version">0.9.0</td> - way better



let version = document.getElementById('app-version').innerText;
window.location = `/App/App_${version}.apk`; // now with generate /App/App_0.9.0.apk


Edit



<a href="#" data-version="0.9.0" onclick="version = this.getAttribute('data-version');">Download</a>


Edit2



If you want to format string in JS using ${} then string must be in ``



`/App/RealcoApp_${version}.apk`





share|improve this answer















Two things to change



<td id="0.9.0">0.9.0</td> - don't use that format as id.



<td id="app-version">0.9.0</td> - way better



let version = document.getElementById('app-version').innerText;
window.location = `/App/App_${version}.apk`; // now with generate /App/App_0.9.0.apk


Edit



<a href="#" data-version="0.9.0" onclick="version = this.getAttribute('data-version');">Download</a>


Edit2



If you want to format string in JS using ${} then string must be in ``



`/App/RealcoApp_${version}.apk`






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 at 17:10

























answered Jan 4 at 16:50









Oen44Oen44

1,8291919




1,8291919













  • should i put "let version = document.getElementById('app-version').innerText;" in the onClick? Actually i have to change the id for each td or i doesnot? as i have a lot of td with the different versions

    – JohnKarry
    Jan 4 at 16:53






  • 1





    id must be unique. If you are using button then you can use data attribute on that button and set version number there.

    – Oen44
    Jan 4 at 16:56






  • 1





    Made edit, should help.

    – Oen44
    Jan 4 at 17:01






  • 1





    just solved by using ' /App/RealcoApp_' + version +'.apk' ' thank you :)

    – JohnKarry
    Jan 4 at 17:16






  • 1





    That will work too, good job.

    – Oen44
    Jan 4 at 17:17



















  • should i put "let version = document.getElementById('app-version').innerText;" in the onClick? Actually i have to change the id for each td or i doesnot? as i have a lot of td with the different versions

    – JohnKarry
    Jan 4 at 16:53






  • 1





    id must be unique. If you are using button then you can use data attribute on that button and set version number there.

    – Oen44
    Jan 4 at 16:56






  • 1





    Made edit, should help.

    – Oen44
    Jan 4 at 17:01






  • 1





    just solved by using ' /App/RealcoApp_' + version +'.apk' ' thank you :)

    – JohnKarry
    Jan 4 at 17:16






  • 1





    That will work too, good job.

    – Oen44
    Jan 4 at 17:17

















should i put "let version = document.getElementById('app-version').innerText;" in the onClick? Actually i have to change the id for each td or i doesnot? as i have a lot of td with the different versions

– JohnKarry
Jan 4 at 16:53





should i put "let version = document.getElementById('app-version').innerText;" in the onClick? Actually i have to change the id for each td or i doesnot? as i have a lot of td with the different versions

– JohnKarry
Jan 4 at 16:53




1




1





id must be unique. If you are using button then you can use data attribute on that button and set version number there.

– Oen44
Jan 4 at 16:56





id must be unique. If you are using button then you can use data attribute on that button and set version number there.

– Oen44
Jan 4 at 16:56




1




1





Made edit, should help.

– Oen44
Jan 4 at 17:01





Made edit, should help.

– Oen44
Jan 4 at 17:01




1




1





just solved by using ' /App/RealcoApp_' + version +'.apk' ' thank you :)

– JohnKarry
Jan 4 at 17:16





just solved by using ' /App/RealcoApp_' + version +'.apk' ' thank you :)

– JohnKarry
Jan 4 at 17:16




1




1





That will work too, good job.

– Oen44
Jan 4 at 17:17





That will work too, good job.

– Oen44
Jan 4 at 17:17




















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%2f54042992%2fhow-to-dynamically-change-var-value-on-click%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

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas