1
<input type="radio" name="imgsel"  value="" checked />

My requirement is : This radio button by default checked then value of this button is 'present'. When this button is unchecked , then value this button is 'absent'. how can i do this?

please, help me . Thanks in advance .

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79

2 Answers2

1

function valChange(element)
{
   if (element.checked)
  {
     alert("present");
  } else
  {
    alert("absent");
  }
   }
<input id="button" type="checkbox" name="imgsel" 
onchange="valChange(this)" value="" checked />
Aman Sharma
  • 933
  • 1
  • 4
  • 12
0

The following example replaces the value of the checkbox when clicked. First the click event must be bound to the checkbox. The EventHandler checks whether the box has already been checked. If not, it checks it and sets the value in the checkbox. And if it has not been checked, the other way round.

let i = document.querySelector('input');

i.addEventListener('click', (ev) => {
  let el = ev.target;
  
  if (el.getAttribute('checked') === null) {
    el.setAttribute('checked', true);
    el.value = "present"         
  } else {    
    el.removeAttribute('checked');
    el.value = "absent"    
  }
  let checkValue = document.querySelector('input').value
  alert(checkValue)
})
<input type="radio" name="imgsel"  value="" checked />
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79