Can't disable button using Javascript

allanbeth 26 Nov, 2013
hi all,

I am trying to disable a button on my CC list depending on a certain field in the data table.

Here is the code

<?php $state = "{$row['in_marketplace']} " ; ?>
<script>
var x = "<?php echo $state ; ?>" ;
if (x == "yes") 
{
document.getElementById("marketplace").disabled = true; 
}
else  
{
document.getElementById("marketplace").disabled = false; 
 }
</script>


$state will be either yes or no, if its yes, i want to disable the button i have set with ID 'marketplace'

Anyone know why this is not working?

Thanks in advance

Allan
allanbeth 27 Nov, 2013
Thanks for the help, i have put your revised code into a function and calling it from the onload event as below, but i cant get it to work. any ideas?
<script>

function disableBtn() {

window.addEvent('domready', function(){
<?php $state = "{$row['in_marketplace']} " ; ?>
   var x = "<?php echo $state ; ?>" ;
   if (x == "yes")
   {   
      document.getElementById("marketplace").disabled = true;
   }
   else 
   {
      document.getElementById("marketplace").disabled = false;
   }
});
}
window.onload=disableBtn;
</script>
GreyHead 27 Nov, 2013
Hi allanbeth,

Your code structure isn't quite the same as Sloan's, you have the window.addEvent() inside the function so it won't run until the function is called.

I have a more fundamental question. Why are you doing this with JavaScript. If it's running the the CC Body box can't you just set the disabled='disabled' with PHP?

Bob
allanbeth 29 Nov, 2013
Hi Bob,

Not sure i can answer the question🙂 Still Learning!!🙂

You are, of course 100% correct, i was making it more complicated than i needs to be.

I am now doing it using PHP. plus i am not disabling the button, but hiding it using the following
<?php if ($row['in_marketplace'] == "yes") echo 'style="display:none;"' ?>


I had to use the hide method as i am using the buttons as links for a modal window so the disabled method didn't work for me as the button is being treated like a link. (Think i have that correct.)

Thanks for all your help

Allan
GreyHead 29 Nov, 2013
Hi allanbeth,

Looks OK to me.

Bob
This topic is locked and no more replies can be posted.