Hi all,
I've got a script that I found on these forums that gives a text-box to a set of radio options. It's essentially an 'other___' box.
It works great, however, my php contains a loop where the strip of radio options would appear multiple times on one page. Not knowing too much javascript, I was hoping someone could help me generalize this JS so it can exist in multiple instances on the page.
JS
Code:
function displayTextBox()
{
var objElement = document.getElementById('otherTextBox');
otherTextBox.style.display = 'block';
otherTextBox.style.visibility = 'visible';
}
function hideTextBox()
{
var objElement = document.getElementById('otherTextBox');
otherTextBox.style.display = 'none';
otherTextBox.style.visibility = 'hidden';
}
function validate()
{
var arrElements = document.getElementsByName('choice');
var objElement;
var boolContinue = false;
var objOtherText;
for(var i=0, _length=arrElements.length; i<_length; i++)
{
objElement = arrElements[i];
if(objElement.checked)
{
if(objElement.id == 'choice5')
{
objOtherText = document.getElementById('othertext');
if(strTrim(objOtherText.value).length>0)
{
boolContinue = true;
break;
}
}
else
{
boolContinue = true;
break;
}
}
}
if(boolContinue)
{
alert('Continue, user completed the information.')
}
else
{
alert('Ask user to complete the data.')
}
}
/**
* Removes all white space characters from the string.
*
* @param: {String} String to trim.
*
* @return {String} Trimed string.
*/
function strTrim(strTrim)
{
return strTrim.replace(/^\s+|\s+$/g, '');
}
Is there not a way to have to javascript seek out the correct text box by using 'THIS' or some variation?
I worry that giving every instance a unique name would quickly become problematic when it comes time to passing the form to the database.
Bookmarks