Jump to content

preselected radio button on page load


Guest ashwinkudwa

Recommended Posts

Guest ashwinkudwa

Does anybody know as to how can i preselect the topmost radio button among the many dynamically generated radio buttons when the page loads with the help of html:radio tag. :)

Link to comment
Share on other sites

if they are dynamically generated through a for loop or some thing do this

for(i=0;i<10;i++){    if(i==0)    {         radioHtml = "<input type=radio selected>";    }    else    {         radioHtml = "<input type=radio>";    }    Response.Write(radioHtml);}

Link to comment
Share on other sites

And if you like to write in newer HTML, called Xhtml, and for the difference execute JavaScript instead of VBScript, you could do it like this:

for (i=0; i<10; i++){  if (i==0)  {      radioHtml = '<input type="radio" selected="selected">';  }  else  {      radioHtml = '<input type="radio">';  }  document.write(radioHtml);}
:)Oh, and before I forget, you can comprimate all this to just thes lines:
for (i=0; i<10; i++){ radioHtml = '<input type="radio"' . ( (i ==0) ?' selected="selected"' :'' ) . ' />';} document.write(radioHtml);
Edited by Dan The Prof
Link to comment
Share on other sites

  • 5 years later...

I found that the attribute "checked" worked for me as in :

<input type="radio" name="upc" value="upc" checked /> Include UPC<br />

Link to comment
Share on other sites

whoa! >.<I guess that's what you get for just selecting the read last post orange box, haha. :)

Link to comment
Share on other sites

thescientist, good point on the checked="checked". I'm sure you just helped me avoid a validation issue. Anyway, I was just trying to make use of the archive. Interestingly, it was the only topic that came back on my search. I expected more topics on the subject.

Edited by niche
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...