Jump to content

Recommended Posts

Hi, the javaScript should have an alert popup indicating the ith <option> selected (the index), but its not working.

 

Here's the HTML:

<!DOCTYPE html>

<html>

<head><title>HTML5, CSS3, CSS, JavaScript, AJAX practice</title>

<script type="text/javascript" src="js_.js" ></script>

</head>

<body>

<form id="form_2">

<br /><br />

<select id="select_1" size="4"

onchange="whichPet(document.getElementById('select_1'));">

<option>golden retriever</option>

<option>siamese cat</option>

<option>goldfish</option>

<option>parrot</option>

</select>

</form>

 

</body>

</html>

 

here's the JS in external script: js_.js

function whichPet(var petList)
{
alert("You selected " + petList.options[selectedIndex].text);
}

Link to comment
https://forums.phpfreaks.com/topic/254310-help-with-selection-and-alert/
Share on other sites

i guess i'll show you, im going to add values to your options, which should be done anyway.

 

<!DOCTYPE html>
<html>
<head><title>HTML5, CSS3, CSS, JavaScript, AJAX practice</title>
   <script type="text/javascript" src="js_.js" ></script>
</head>
<body>
<form id="form_2">
   <br /><br />
   <select id="select_1" size="4" 
      onchange="whichPet(document.getElementById('select_1').selectedIndex);">
      <option value='golden retriever'>golden retriever</option>
      <option value='siamese cat'>siamese cat</option>
      <option value='goldfish'>goldfish</option>
      <option value='parrot'>parrot</option>
   </select>
</form>

</body>
</html>

 

external

 

function whichPet(petListIndex)
{
        var obj = document.getelementById("select_1");
alert("You selected " + obj.options[petListIndex].value);
}

Thanks, it works now. I was reading a javascript tutorial since w3schools is dry and boring, but that tutorial for the most part was good (it was written in 2010), but it said to use:

<select name="choose_category" onchange="swapOptions(window.document.the_form.choose_category.options[selectedIndex].text);">

 

and it did not include text property in the <option> tag.

 

 

Thanks, it works now. I was reading a javascript tutorial since w3schools is dry and boring, but that tutorial for the most part was good (it was written in 2010), but it said to use:

<select name="choose_category" onchange="swapOptions(window.document.the_form.choose_category.options[selectedIndex].text);">

 

and it did not include text property in the <option> tag.

.text will work technically, however you should always include a back end alternative for those that do not have javascript enabled in their browser, so you would want to have values for your <option>s anyway.

i guess i'll show you, im going to add values to your options, which should be done anyway.

 

<!DOCTYPE html>
<html>
<head><title>HTML5, CSS3, CSS, JavaScript, AJAX practice</title>
   <script type="text/javascript" src="js_.js" ></script>
</head>
<body>
<form id="form_2">
   <br /><br />
   <select id="select_1" size="4" 
      onchange="whichPet(document.getElementById('select_1').selectedIndex);">
      <option value='golden retriever'>golden retriever</option>
      <option value='siamese cat'>siamese cat</option>
      <option value='goldfish'>goldfish</option>
      <option value='parrot'>parrot</option>
   </select>
</form>

</body>
</html>

 

external

 

function whichPet(petListIndex)
{
        var obj = document.getelementById("select_1");
alert("You selected " + obj.options[petListIndex].value);
}

 

Why do you manually create an object instead of using this, then only pass in the selectedIndex so that you have to recreate the same object within the function, when you could have just passed it in?

 

whichPet(this);

 

function whichPet(select)
{
    alert("You selected " + select.options[select.selectedIndex].value);
}

i guess i'll show you, im going to add values to your options, which should be done anyway.

 

<!DOCTYPE html>
<html>
<head><title>HTML5, CSS3, CSS, JavaScript, AJAX practice</title>
   <script type="text/javascript" src="js_.js" ></script>
</head>
<body>
<form id="form_2">
   <br /><br />
   <select id="select_1" size="4" 
      onchange="whichPet(document.getElementById('select_1').selectedIndex);">
      <option value='golden retriever'>golden retriever</option>
      <option value='siamese cat'>siamese cat</option>
      <option value='goldfish'>goldfish</option>
      <option value='parrot'>parrot</option>
   </select>
</form>

</body>
</html>

 

external

 

function whichPet(petListIndex)
{
        var obj = document.getelementById("select_1");
alert("You selected " + obj.options[petListIndex].value);
}

 

Why do you manually create an object instead of using this, then only pass in the selectedIndex so that you have to recreate the same object within the function, when you could have just passed it in?

 

whichPet(this);

 

function whichPet(select)
{
    alert("You selected " + select.options[select.selectedIndex].value);
}

yeah, thumbs up

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.