Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-21-2012, 10:43 PM   PM User | #16
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
Again, without enough information as to what it should look like, my mind reading skills suck!

Show some work to comment upon.
jmrker is offline   Reply With Quote
Old 11-23-2012, 04:34 PM   PM User | #17
linkysnake
New Coder

 
Join Date: Nov 2012
Posts: 11
Thanks: 7
Thanked 0 Times in 0 Posts
linkysnake is an unknown quantity at this point
First of all, thanks for your patience.....

When i say level options i mean this

1st Level Options (Andalucía, Aragón, Asturias, Baleares)
2nd level options (the ones in the array)

The first thing i was looking for was a select list with 2 levels with an automatic generated link with a BaseURl + the name of the options, you solved it giving me this code:

Works perfect, but i don't know how to make it to open the links in a new tab instead the same window, that's the only problem i have with that. Hope you can help me with that...

Code:
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8" />
<title> Untitled </title>

<script type="text/javascript">

// NOTE: You need to fill in the links as defined by your requirements in the following array elements
var provincias = [
  [],  // spare (not used in this version of program)
  ['-','Almería',
       'Cádiz',
       'Córdoba',
       'Granada',
       'Huelva',
       'Jaén',
       'Málaga',
       'Sevilla'],
  ['-','Huesca',
       'Teruel',
       'Zaragoza'],
  ['-','Asturias'],
  ['-','Baleares']  // Note: no comma after final entry
];

function cambiar(formulario){
  var i = 0;
  var select1 = formulario['D1'];
  var select2 = formulario['D2'];
  var vector = provincias[select1.selectedIndex];
  if(vector.length)select2.length=vector.length;
  var tarr = [];
  while(vector[i]){
    select2.options[i].value = vector[i]; // tarr[1];
    select2.options[i].text = vector[i];  // tarr[0];
    i++;
  }
  select2.options[0].selected = 1;
}
function goToSite(info) {
  if (info != '-') { alert(info); }                       // this line is for testing purposes only
//  if (info != '-') { document.location.href = info; }   // uncomment this line after testing
}

function linkToSite(formulario) {
  var select1 = formulario['D1'];
  var select2 = formulario['D2'];
  var baseURL = 'http://www.codingforums.com/';  // defined as some common URL if same for all links
  var str = '';
  str = baseURL+select1.value+select2.value+'.html';
  goToSite(str); 
}
</script>

<style type="text/css">

</style>
</head>
<body>
<form method="POST">
  <select name="D1" onchange="cambiar(this.form)">
  <option>-</option>
  <option>Andalucía</option>
  <option>Aragón</option>
  <option>Asturias</option>
  <option>Baleares</option>
  </select>
  <select name="D2" onchange="linkToSite(this.form)">
  </select></p>
</form>
</body>
</html>
Second

I thought that it would be easy to me to modify the code and eliminate the second level options, to just keep 1 level option but with generating and taking me to the link with a "BaseUrl too + the option", in other words, the same thing that the code i post uo here but just for 1 level option, but i can't, and i don't post any code of that because i suck in javascript stuff..

Thank you
linkysnake is offline   Reply With Quote
Old 11-23-2012, 09:23 PM   PM User | #18
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
For your 1st problem, look into: http://www.w3schools.com/jsref/met_win_open.asp
That site shows you all the new window options available from javascript.

Second part.
Are you expecting for the level one option to do 2 different actions?
Sometimes, it gives you only one link and another time it gives you a different link?
If that is the case, then you need to define for me which links are controlled by which level at the time a selection is made.

OR, are you asking for a SECOND drop down list that has only the first level and no sub-levels to control?
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
linkysnake (11-26-2012)
Old 11-26-2012, 03:36 PM   PM User | #19
linkysnake
New Coder

 
Join Date: Nov 2012
Posts: 11
Thanks: 7
Thanked 0 Times in 0 Posts
linkysnake is an unknown quantity at this point
The first problem is solved now...there's just a detail, Internet Explorer take me to the link, bur without adding the first level option to it, Any idea why it is working on Chrome but not in IE?


Quote:
Originally Posted by jmrker View Post
OR, are you asking for a SECOND drop down list that has only the first level and no sub-levels to control?
Yes, i'm asking for another drop down list with only the first level and no sublevels (but with generated links "BaseUrl+Option")


Thank You

Last edited by linkysnake; 11-26-2012 at 04:25 PM..
linkysnake is offline   Reply With Quote
Old 11-27-2012, 03:29 PM   PM User | #20
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
Lightbulb

Add this to code...

HTML:
Code:
  <select name="D3" onchange="singleLinkToSite(this.form)">
  <option value='-'>-</option>
  <option value='Andalucia'>Andalucía</option>
  <option value='Aragon'>Aragón</option>
  <option value='Asturias'>Asturias</option>
  <option value='Baleares'>Baleares</option>
  </select>
  <p />
JS:
Code:
function singleLinkToSite(formulario) {
  var select1 = formulario['D3'];

// defined as some common URL if same for all links
  var baseURL = 'http://www.codingforums.com/';  
// could be a 'global' variable if used in multiple functions

  var str = '';
  str = baseURL+select1.value+'.html';
  goToSite(str); 
}
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
linkysnake (11-27-2012)
Old 11-27-2012, 03:39 PM   PM User | #21
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 122
Thanks: 0
Thanked 18 Times in 16 Posts
007julien is an unknown quantity at this point
A good method to build a select menu is to use the Option() constructor for the select element.
Code:
new Option(text, value, defaultSelected, selected)
See this page w3cshools.com page Changing Select element content on the fly.
007julien is offline   Reply With Quote
Users who have thanked 007julien for this post:
linkysnake (11-27-2012)
Old 11-27-2012, 04:38 PM   PM User | #22
linkysnake
New Coder

 
Join Date: Nov 2012
Posts: 11
Thanks: 7
Thanked 0 Times in 0 Posts
linkysnake is an unknown quantity at this point
Thank you, just 10 min before your post i was able to know how to modify the original code and adapt it to just one drop list


You help me a lot and i learn so much things in these days, thank you for all your time and Help.

Thank you too 007julien i will read that to see how it works
linkysnake is offline   Reply With Quote
Reply

Bookmarks

Tags
array, link, list, select

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:00 AM.


Advertisement
Log in to turn off these ads.