Jump to content

Change "readonly" value on link click


dmhall0

Recommended Posts

I have a standard html form that displays numeric values for 8 different fields.  These increase based on other information and are typically marked as "readonly".

I want to add a RESET link next to each field that allows the user to set these back to 0 (zero); then I have php UPDATE code already completed that updates the database.

 

I assume I need a javascript function that does this, but my knowledge in this area is pretty much NULL.

 

<a onclick="jsfunction" href="javascript:void(0);">Reset Value</a>

 

Help with the function would be GREAT!

Thanks

Link to comment
https://forums.phpfreaks.com/topic/256204-change-readonly-value-on-link-click/
Share on other sites


<script type="text/javascript">
<!--
window.addEventListener('load', function() {
   var anchors = document.getElementsByTagName('a');
   for( var k in anchors )
   {
      if ( anchors[k].className.substring(0,5).toLowerCase() == 'reset' )
      {
         anchors[k].addEventListener('click', function() {
	    
            document.getElementById(this.className.substring(5).toLowerCase()).value = 0;
         });
      }
   }
});
//-->
</script>
<input type="text" name="test" id="test" value="4" disabled="disabled" ><a href="javascript:void(0);" class="resetTest" >Clear</a>

Did you change the link's class to reset followed by the ID of the input you want it to change?

 

 

E.g.

 

 

Say your input has an id of  numberofapples (id="numberofapples") the link should have a class of resetnumberofapples (class="resetnumberofapples"), class is case insensitive, but ID MUST BE LOWER CASE.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.