Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining two scripts together

Status
Not open for further replies.

leonk09

Technical User
Nov 9, 2005
20
GB
I have two scripts which work on the same form:

Script one combines the results of two radio buttons to create a combined menu:

<!-- Begin
site="function combineMenus(frm, menu1, menu2) {
with (frm) {
for (var i = 0; i < document.menufrm.menu1.length; i++) {
if (document.menufrm.menu1.checked) {
str = document.menufrm.menu1.value;
break;
}
}
for (var i = 0; i < document.menufrm.menu2.length; i++) {
if (document.menufrm.menu2.checked) {
str += document.menufrm.menu2.value;
break;
}
}
url = site + "/" + str + ".html";
window.location.href = url;
}
}
// End -->

The second script is to validate the menus:

<!-- Begin
function checkRadios() {
var el = document.forms[0].elements;
for(var i = 0 ; i < el.length ; ++i) {
if(el.type == "radio") {
var radiogroup = el[el.name]; // get the whole set of radio buttons.
var itemchecked = false;
for(var j = 0 ; j < radiogroup.length ; ++j) {
if(radiogroup[j].checked) {
itemchecked = true;
break;
}
}
if(!itemchecked) {
alert("Please choose an answer for "+el.name+".");
if(el.focus)
el.focus();
return false;
}
}
}
return true;
}
// End -->

Can anyone advise on a way to combine this scripts into one fully working script?

many thanks
 
What's not working? I see nothing in either script which excludes or negates the other.
Code:
<script type="text/javascript">

site="[URL unfurl="true"]http://javascript.internet.com";[/URL]
function combineMenus(frm, menu1, menu2) 
{
  with (frm) 
  {
    for (var i = 0; i < document.menufrm.menu1.length; i++) 
    {
      if (document.menufrm.menu1[i].checked) 
      {
        str = document.menufrm.menu1[i].value;
        break;
      }
    }
    for (var i = 0; i < document.menufrm.menu2.length; i++) 
    {
      if (document.menufrm.menu2[i].checked) {
        str += document.menufrm.menu2[i].value;
        break;
      }
    }
    url = site + "/" + str + ".html";
    window.location.href = url;
  }
}


function checkRadios() 
{
  var el = document.forms[0].elements;
  for(var i = 0 ; i < el.length ; ++i) 
  {
    if(el[i].type == "radio") 
    {
      var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
      var itemchecked = false;
      for(var j = 0 ; j < radiogroup.length ; ++j) 
      {
        if(radiogroup[j].checked) 
        {
          itemchecked = true;
          break;
        }
      }
      
      if(!itemchecked) 
      {
        alert("Please choose an answer for "+el[i].name+".");
        if(el[i].focus)
          el[i].focus();
        return false;
      }
    }
  }
  return true;
}

</script>

In what way do they not work? What errors do you have?

By the way, please use [ignore]
Code:
[/ignore]
tags to surround code examples to improve their readability.

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
Sorry, will use the code tags.

I get errors from script 1 when no option is selected.

I was hoping that script 2 would validate this error and alert the user before this happened, but instead I get a javascript error (from script 1).
 
I get errors from script 1 when no option is selected.
Remember, we're programmers, not psychics. Any chance of a helpful error message?

Oh, I can't see the [tt]with(frm)[/tt] statement being useful in script one, or any of the variables [tt]frm, menu1, menu2[/tt].

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
The error I'm getting is:

'str' is undefined. Referring to script 1 as no values have been selected for this script to combine.
 
That error is easily fixed - it's because the variable [tt]str[/tt] is only defined within [tt]if[/tt] statements:
Code:
function combineMenus(frm, menu1, menu2)
{
  [COLOR=red]var str="";[/color]
  with (frm)
  {
    for (var i = 0; i < document.menufrm.menu1.length; i++)
    {
      if (document.menufrm.menu1[i].checked)
      {
        str = document.menufrm.menu1[i].value;
        break;
      }
    }
    for (var i = 0; i < document.menufrm.menu2.length; i++)
    {
      if (document.menufrm.menu2[i].checked) {
        str += document.menufrm.menu2[i].value;
        break;
      }
    }
    [COLOR=red]if (str!="") {[/color]
      url = site + "/" + str + ".html";
      window.location.href = url;
    [COLOR=red]}[/color]
  }
}

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top