1

Set choice field value using java script :

I have a choice field with values :
Open
Closed
Cancelled

When my page loads, my choice column should be selected as "Cancelled"(i don't want to set using OOTB default value option.)

2
  • In which kind of Context, a Form ? Listview? Commented Jun 4, 2012 at 7:26
  • ootb list form say, editform.aspx Commented Jun 4, 2012 at 7:37

1 Answer 1

1

Imagine you have the following radio buttons:

<input type="radio" name="color" value="pink" /> Pink 
<input type="radio" name="color" value="black" /> Black  

You can select the radio button using the following jQuery code. Insert this code in EditForm.aspx and you're good to go.

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript">

$(document).ready(function()
{
    var presetValue = "black";
    $("[name=color]").filter("[value="+presetValue+"]").prop("checked",true);
});
</script>

Here is the JS only code:

Refer the JS in the EditForm.aspx:

Try this in your myjs.js file:

 var inputs = document.getElementsByTagName("radio");
 var i = 0;
 for (i = 0; i < inputs.length; i++) {
 var input = inputs[i];
 if (input.id.indexOf('color') > -1)     
    document.getElementById('pink').checked = false;
    document.getElementById('black').checked = true;
4
  • can i do it with javascript only.No jquery Commented Jun 4, 2012 at 8:18
  • What is the glitch while using jQuery? Commented Jun 4, 2012 at 8:23
  • for the particular requirement i can only use javascript. Commented Jun 4, 2012 at 8:25
  • try my updated response. Note, I haven't tested it myself so please feel free to improve it. Commented Jun 4, 2012 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.