CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Link in array list (http://www.codingforums.com/showthread.php?t=282188)

linkysnake 11-14-2012 07:43 PM

Link in array list
 
Hi, i want to put a link in the last option of this select list(the array element, like Cádiz, Córdoba,etc), depending of the option the link must be diferent, the problem is i have no idea how to do that i'm a noob in javascript topic.

Here´s an example of my list:

Javascript:

Code:

<script languaje="JavaScript">
<!--
provincias = new Array();
provincias[0] = new Array();
provincias[1] = new Array('Almería','Cádiz','Córdoba','Granada','Huelva','Jaén','Málaga','Sevilla');
provincias[2] = new Array('Huesca','Teruel','Zaragoza');
provincias[3] = new Array('Asturias');
provincias[4] = new Array('Baleares');
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;
  while(vector[i]){
    select2.options[i].value = vector[i];
    select2.options[i].text = vector[i];
    i++;
  }
  select2.options[0].selected = 1;
}
-->
</script>

HTML:

Code:

<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">
  <option>-</option>
  </select></p>
</form>

Thank you

PD: sorry for my bad english

jmrker 11-15-2012 03:09 AM

Quote:

Originally Posted by linkysnake (Post 1291979)
Hi, i want to put a link in the last option of this select list(the array element, like Cádiz, Córdoba,etc), depending of the option the link must be diferent, the problem is i have no idea how to do that i'm a noob in javascript topic.

Here´s an example of my list:

Javascript:

Code:

<script languaje="JavaScript">
<!--
... VERY OLD code and language is mis-spelled.
-->
</script>

HTML:

Code:

...
Thank you

PD: sorry for my bad english

I am unclear about your first statement. Only (?) the last element of the select list?

This is my best guess as to what you are trying to do.
Note: I have only populated the second (D2) select list with some dummy links.
You will need to provide your own to get it to go where you desire.

Code:

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

<script type="text/javascript">
provincias = new Array();
provincias[0] = [];
provincias[1] = ['-|-','Almería|-','Cádiz|-','Córdoba|-','Granada|-','Huelva|-','Jaén|-','Málaga|-','Sevilla|-'];
provincias[2] = ['-|-','Huesca|http://www.webdeveloper.com','Teruel|http://www.codingforums.com','Zaragoza|http://www.dreamincode.net'];
provincias[3] = ['-|-','Asturias|-'];
provincias[4] = ['-|-','Baleares|-'];

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]){
    tarr = vector[i].split('|');
    select2.options[i].value = tarr[1];
    select2.options[i].text = tarr[0];
    i++;
  }
  select2.options[0].selected = 1;
}
function linkToSite(info) {
  if (info != '-') { alert(info); }  // this line is for testing purposes only
  if (info != '-') { document.location.href = info; }
}
</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.value)">
  </select></p>
</form>
</body>
</html>

Post back if you have questions about my changes.

linkysnake 11-15-2012 05:06 AM

Thank you so much, thats what i was talking about...just another question...is there anyway to generate the link taking the first and the second option with a base url, this is what i mean:

baseurl+option1+option2.html

something like that?? i'm new in this stuff acctually i don't know anything, but i like to take a look and see how the code works (or at least the mayor part of the code jeje).

Thank you.

jmrker 11-15-2012 05:10 PM

Quote:

Originally Posted by linkysnake (Post 1292123)
Thank you so much, thats what i was talking about...just another question...is there anyway to generate the link taking the first and the second option with a base url, this is what i mean:

baseurl+option1+option2.html

something like that?? i'm new in this stuff acctually i don't know anything, but i like to take a look and see how the code works (or at least the mayor part of the code jeje).

Thank you.

Minor changes:
Code:

function linkToSite(formulario) {
  var select1 = formulario['D1'];
  var select2 = formulario['D2'];
  alert('baseURL '+select1.value+' '+select2.value);
}

...

  <select name="D2" onchange="linkToSite(this.form)">

And remove un-needed URL portions from 'provincias' array elements

Note: for consistency, you could change array assignment to:
Code:

var provincias = [];

linkysnake 11-20-2012 04:23 PM

Thank you so much :thumbsup:

jmrker 11-20-2012 04:45 PM

Quote:

Originally Posted by linkysnake (Post 1293469)
Thank you so much :thumbsup:

You're most welcome.
Happy to help.
Good Luck!
:)

linkysnake 11-20-2012 04:49 PM

Can you post the complete code with the minor changes? The first time it works but just 1 time, i think i made the changes in the wrong part or something...

Thank you so much again :)

jmrker 11-20-2012 06:42 PM

Quote:

Originally Posted by linkysnake (Post 1293477)
Can you post the complete code with the minor changes? The first time it works but just 1 time, i think i made the changes in the wrong part or something...

Thank you so much again :)

The code I would post is already there in post #2 and #4.
It would be better if you posted the code you are using to see what it is that you are doing wrong.

linkysnake 11-20-2012 07:30 PM

Ok, the problem is i don't have idea of where to place the minor changes in the post #4 ,that's why i'm asking you for the complete example code :confused:..... i'm using this one:

Code:

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

<script type="text/javascript">
provincias = new Array();
provincias[0] = [];
provincias[1] = ['-|-','Almería|-','Cádiz|-','Córdoba|-','Granada|-','Huelva|-','Jaén|-','Málaga|-','Sevilla|-'];
provincias[2] = ['-|-','Huesca|http://www.webdeveloper.com','Teruel|http://www.codingforums.com','Zaragoza|http://www.dreamincode.net'];
provincias[3] = ['-|-','Asturias|-'];
provincias[4] = ['-|-','Baleares|-'];

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]){
    tarr = vector[i].split('|');
    select2.options[i].value = tarr[1];
    select2.options[i].text = tarr[0];
    i++;
  }
  select2.options[0].selected = 1;
}
function linkToSite(info) {
  if (info != '-') { alert(info); }  // this line is for testing purposes only
  if (info != '-') { document.location.href = info; }
}

function linkToSite(formulario) {
  var select1 = formulario['D1'];
  var select2 = formulario['D2'];
  alert('baseURL '+select1.value+' '+select2.value);
}

</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.value)">
  </select></p>
</form>
</body>
</html>

thank you again :D

jmrker 11-20-2012 08:13 PM

The program will do very little for you until you fill in the appropriate links in the 'provincias' array.
In the following I have only set-up dummy choices for:
'Aragon' and 'Huesca' or 'Teruel' or 'Zaragoza'

There are no other elements initialized. Their selection will only show a blank '-' alert
Also, since they would link to different baseURL locations, I have left it defined as a blank because you have not provided it as requested.

Finally, I have used an alert to display the link to show where it would go to.
I do not go to it unless you uncomment the document.href.location = ???somewhere??? line.

You have not provided me enough information to complete the project for you and my mind reading skills suck!

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|http://www.webdeveloper.com',
        'Teruel|http://www.codingforums.com',
        'Zaragoza|http://www.dreamincode.net'],
  ['-|-','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]){
    tarr = vector[i].split('|');
    select2.options[i].value = tarr[1];
    select2.options[i].text = tarr[0];
    i++;
  }
  select2.options[0].selected = 1;
}
function linkToSite(info) {
  if (info != '-') { alert(info); }  // this line is for testing purposes only
  if (info != '-') { document.location.href = info; }
}

function linkToSite(formulario) {
  var select1 = formulario['D1'];
  var select2 = formulario['D2'];
  var baseURL = '';  // defined as some common URL if same for all links
  var str = '';
  str = select2.value;  // add in baseURL if defined and the same for all links (???)
  alert(str);          // remove after testing

// following can be substituted, ONCE DEFINED, to go to link selected
//  str += baseURL+' - '+select1.value + ' - ' + select2.value;
//  document.href.location = str;  // to go to link chosen
}
</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>


linkysnake 11-20-2012 08:37 PM

Sorry for the inconveniences :(, i will explain what i want to do the mos clearly i can.

I have the list with 2 options that take me to link i defined (the first code you provide me in the #2 post) and works fine, but what i'm asking for now is something like this:

let's imagine i choose: Andalucia and then Almeria

i want a base url to be the same to all the options no matter wich i choose, mmm like: www.webdeveloper.com

but the link when i choose Almeria must take me to:

www.webdeveloper.com/AndaluciaAlmeria.html

BaseUrl+option1+option2

did i explain myself ok? hope this is clearly sorry my english is not that good...i will ask you for a complete code like the first one because if you indicate me the changes probably i will not be able to understand where to place it.

and Again Thanks for all your help :)

jmrker 11-20-2012 11:29 PM

Be sure to change the 'baseURL' to your common site or you'll get a lot of 'file not found' errors.
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|Almería',
        'Cádiz|Cádiz',
        'Córdoba|Córdoba',
        'Granada|Granada',
        'Huelva|Huelva',
        'Jaén|Jaén',
        'Málaga|Málaga',
        'Sevilla|Sevilla'],
  ['-|-','Huesca|Huesca',
        'Teruel|Teruel',
        'Zaragoza|Zaragoza'],
  ['-|-','Asturias|Asturias'],
  ['-|-','Baleares|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]){
    tarr = vector[i].split('|');
    select2.options[i].value = tarr[1];
    select2.options[i].text = 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>

Could be simplified even more with you last requirement,
but not worth the effort if you are happy with the results.

linkysnake 11-21-2012 03:54 PM

well, with that i need to add to every second option the word i want to add to the link...i was wondering if there's a way to add the actual option1 and option 2, not typing again option 2 like this:

Zaragoza|Zaragoza (if 'm right the bold one will be added to the base URL)

But i want the option i choose adds to the baseurl to save me time typing because my list have like 30 options with 12 sub-options each one, that leaves me with 360 links (actually each one will search for a file to download).

For some reason the code only adds the option2 value to the base URL, again thank you very much for your time you're a Master :thumbsup:

jmrker 11-21-2012 07:06 PM

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>

Quote:

For some reason the code only adds the option2 value to the base URL,
I don't understand your last statement. I works as you requested as far as I can see.

linkysnake 11-21-2012 07:57 PM

sorry, i check it again and it works fine, i don't know why it wasn't working, my mistake, thank you for all your help i think i solve my problem with your help, now i just need to modify the code because i need some lists without sub-options just and array with one list and baseUrl, i hope i can do that because i have no idea of how to create an alone array in javascript XD, have a good day and thanks for all :)


Update: damn i can't get rid of one list, i only want a simple list with BaseUrl+option1 (with out option 2 list) but i can't i'm not that good with codes :/ hope you can help me...again...


All times are GMT +1. The time now is 05:20 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.