Jomla
Jomla

Reputation: 93

How to change button ID to another ID using Javascript?

Based on my snippet code, I'm having error in TypeError because of ID is not exist, the button ID can be set in admin by yes, no, disabled,

$(document).ready(function(){
        document.getElementById("no").remove();
        document.getElementById("disabled").disabled = true;
        document.getElementById('yes').setAttribute('id','<?php echo $update->update;?>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!--<button id="<?php echo $update->install;?>">Install</button>-->
<button id="disabled">Install</button>

My questions:

1) How to solve TypeError when ID is not exists?

2) How to show value of <?php echo $update->update;?> inside JavaScript?

Upvotes: 1

Views: 123

Answers (1)

Eddie
Eddie

Reputation: 26844

Since you are using jQuery, just use it to fix the TypeError issue. jQuery already does the checking.

You can '<?php echo $update->update;?>' to echo on js.

$(document).ready(function(){
     $( "#no" ).remove();
     $( "#no" ).attr('id','done');
     $( '#disabled' ).prop('disabled', '<?php echo $update->update;?>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!--<button id="<?php echo $update->install;?>">Install</button>-->
<button id="disabled">Install</button>


Note: if you are trying to set the initial status of a button, it is recommended to set it on php and not on jQuery.

$status = "disabled";
echo '<button type="button" ' . $status . '>Click Me!</button>';

Upvotes: 3

Related Questions