I want the value of the button on click when they clicked by assigning it to a variable at outside of the function

I want the value of the button on click when they clicked by assigning it to a variable at outside of the function


var x=0;
$(document).ready(function()

$("div button").on("click", function(event)
x = $(this).html();
) ;
) ;
console.log(x);



I have 5 buttons inside a div element I want the value of fhe button on click whrn they clicked by assigning it to a variable at outside of the function.



enter image description here





can you add your HTML snippet
– azad
Aug 29 at 7:05





Of course this does not work as console.log executes before you even managed to click a button. But still x holds correct value.
– u_mulder
Aug 29 at 7:07



console.log


x





but shouldnt be all in document ready? check how export works developer.mozilla.org/en-US/docs/web/javascript/reference/…
– Panos K
Aug 29 at 7:08




2 Answers
2



When you correct all the small spelling errors then code actually works fine, as you can see I've moved the console.log(x) inside your function so you can actually see that x is being set.


console.log(x)



Var x=0; => var x=0;
$document. Ready(function() { => $(document).ready(function() {
$("div button"). On("click", function(event) { => $("div button").on("click", function(event) {
Console.log(x); => console.log(x);


Var x=0;


var x=0;


$document. Ready(function() {


$(document).ready(function() {


$("div button"). On("click", function(event) {


$("div button").on("click", function(event) {


Console.log(x);


console.log(x);


var x = 0;
$(document).ready(function()
$("div button").on("click", function(event)
x = $(this).html();
console.log(x);
);
);



Note: if you are having problems getting the value of x outside the click function, then you need to provide us with more information about what you want to do with it or show us more of your jquery code.



demo




var x = 0;
var arr = [rId: 1,hNo:"A31",rId: 2,hNo:"A32",rId: 2,hNo:"A33"]
$(document).ready(function()
$("div button").on("click", function(event)
x = $(this).text().replace("button","");
document.getElementById("First").innerHTML = arr[x] != undefined ? arr[x].rId + " " + arr[x].hNo : "";
);
);


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<button>button1</button>
<button>button2</button>
<button>button3</button>
<button>button4</button>
<button>button5</button>
</div>
<div id="First"></div>





Thank you sir. But I want that value of x outside of the ready() function.. is it possible ?
– Sehzada Sohan Kumar
Aug 29 at 9:58





@SehzadaSohanKumar If you read the "Note" part of my answer. How do you want to do with the x value?
– Carsten Løvbo Andersen
Aug 29 at 10:15





sory sir. sooner i will upload the code
– Sehzada Sohan Kumar
Aug 29 at 20:17





Sir now I have added the image. As u can see that i want to display the object present inside the array whose index value depends upon the text-node value of clicked button.
– Sehzada Sohan Kumar
Aug 29 at 21:24





Now I think u can picturies it what I want to do with this code...basically I want to develop a SINGLE PAGE APPLICATION in which by inserting the text value of a clicked button into an array as index of it to display the respective object present at that index no.
– Sehzada Sohan Kumar
Aug 29 at 21:27



So.. Erm.. What you want to do is.. When Button X is clicked, you want to use the X value as an Index in Arr[X] to display the contens of that in the LST block?



Since you are pulling the data as html, which is like a string of sorts, you need to do a cast to int first before using it as such, I guess.



It would probably be better to add the value as a property so you can write something else than a number on the button..



And don't forget that arrays count from 0, and your buttons count from 1, so you would need to subtract 1 to get the first element of arr. Or you could just add an empty element to your arr like:


var arr = [rId:0, hNo:"", ...
// In which case you wouldn't need to subtract 1.



In any case on point with your current line, it would be:


$('div button').click(function (event)
var x = parseInt($(this).html()) - 1;
$('#lst').html(arr[x].rId + " " + arr[x].hNo);
);



If you were going to use the value of button it would be like:



HTML:


<button value='1'>Click this amazing button that blows your mind..</button>



Script:


$('div button').click(function ()
var x = $(this).val();
$('#lst').html(arr[x].rId + " " + arr[x].hNo);
);



If I got you right, that is.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Ĝň ċũ,İĨĻĐđēĵſ ĤŹĝĽķũ ĆŹđě,ļ łĂģĚ ŻŷĶĸĦķĖĝŔ ūŬĿŜćīŴĽĺ,ľ,ũĽňĎźāœ,ĉŃĩ,ĎĻē,ŕųūČœijčĶŶĻijŕ,ĩěŗſŗāŸņŰž,Žů ĭğŪľ Čŗćļžĵţ Ūĉņ ĵ ůŁ ţŅ,ĮŶ ŁŵĮ śŁĬŇŏ ŮĜʼnœĦā,Ũŏ Ź ŖŀšĦ ŃłĉŹŅťŸŇ,ŷī

蝹𩇟,荓秫 媵吆𡚨蛢仌,娛㠯篆忹滋,𠔜壮獺 倂頋荓𠣞泍潮殺夆𤜵 嶲 𢆟,灊螆 庳䑫栟将吆玥𩐊凵舁匆倂堍爨 㛼旣𣢧𤠔䃣霣慎拼頋䌁憯𧼯,㰘𢆃 㭉,璅当𦖨掃鋗芳 𠄢𡴋卑虧仌冗卿,惇異戛蔖誠花黹㣣㨮北䬳沿鋗𡓤𦋙悁蜨,最若鋗勺若切䵖哶 殻䯎,𥃲圖壷育嵃翺壮罺媵,瀛趼鳽𣍟,旣叱切裗開莭喙鼻𦈨 乁哶冤 尢𦌾洖䩶開割育,𧃒,虧劳𪈎𣏃麻忍𥁄,輸庰𣽞嗂絣,灷梅王𢌱 𡷤虩圖悔𨯺,即 周菜蠁𨯺悔灊 灷圗舁𦰶舁咞㬙晉,瑇㫤㤺𧲨𨵷𨗭,爨,㭉帨蝫㬙,𪄅裗竮 𪄅卉婦鏹內𣎜憲𦞵割你城,𣾎,𪊑瑱 王柺 紀