Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing button value on button click

Just starting out in Xpages and ran into a silly problem:

I have a button (id- "button2") and it has label "aaa". What I'm trying to do is to change the value of the button on the button click to value "kappa123". I've included my JavaScript in the "Client" tab in Script Editor.

JavaScript:

var elem = document.getElementById("button2");
if (elem.value=="aaa") elem.value="kappa123";
else elem.value = "aaa";

I don't even a error and nothing happens. What am I doing wrong?

like image 714
MarisP Avatar asked Jun 23 '26 11:06

MarisP


2 Answers

Use

var elem = document.getElementById("#{id:button2}");
if (elem.innerHTML=="aaa") elem.innerHTML="kappa123";
else elem.innerHTML = "aaa";

You can't use the element id in client side code direct as the id gets "renamed" by XPages. With #{id:button2} you get the rendered id.

like image 144
Knut Herrmann Avatar answered Jun 26 '26 01:06

Knut Herrmann


Demo

   jQuery('#button2').click(function(){
   var elem = document.getElementById("button2");
    if (elem.value=="aaa") jQuery("#button2").attr('value', 'If');
     else jQuery("#button2").attr('value', 'Else');
     alert(elem.value);
   });
like image 33
Deep Kakkar Avatar answered Jun 25 '26 23:06

Deep Kakkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!