I want to modify a script for toggling a div's visibility to utilize a select dropdown menu instead of radio buttons

CSS

@charset "utf-8";
/* CSS Document */

body,td,th {
font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;
font-size: medium;
}
body form select {
font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;
font-size: medium;
}
.submit {
font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;
font-size: medium;
}
body form {
text-align: center;
margin-top: 10px;
}
.submission {
margin:0 auto 0 auto;
height:500px;
width:400px;
border:#000000 solid 3px;
float:left;
padding: 20px 0;
}
.center {
margin:0 auto;
width: 812px;
}
.left{
text-align:left;
}
.right{
float:;
}
.eighty{
width:80%;
}
.marg_20{
margin-left:20px;
}
.marg_40{
margin-left:40px;
}
.marg_60{
margin-left:60px;
}
.content{
display:none ;
}
.up20{
margin-top:-20px;
}
.signMain{
width:200px;
height:100px;
background:WhiteSmoke ;
margin:20px auto 0;
}
.test{
width: 198px;
height: 20px;
margin-right: auto;
margin-left: auto;
float: left;
margin-bottom: 5px;
font-size:20px;
text-align:center;
}
/*select
{
color: #ccc;
}
option
{
color: #000;
}
option:first-child
{
color: #ccc;
}
*/
.submitDiv {
height: 100px;
}
.signCreatorDiv {
height: 400px;
}
.namesColumn{
width:200px;
height:350px;
margin:0 auto;
}

javascript

<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$('select[name="Font"]').change(function () {
var idx = $(this).index();
$('.namesColumn input:text').hide().prop("required", false);
$('.namesColumn .NameImg.content').show();
$('.namesColumn .NameImg.content:lt(' + (idx + 1 ) + ') input:text').show().prop("required", true); 
})
});//]]>  

</script>

HTML

<form action="https://im-here.foxycart.com/cart" method="post" accept-charset="utf-8">
<div class="center">
<!--Begin leftSide-->
<div class="submission">
<!--Begin hiddenInputs-->
<input type="hidden" name="name" value="I'm Here Notification Sign" />
<input type="hidden" name="price" value="10" />
<input type="hidden" id="productImage" name="image" value="http://2plygraphics.com/im-here/images/01.jpg"/>
<!--End hiddenInputs-->
<!--Begin colorSelection-->
<div class="left eighty center">
<label>Color</label>
<br>
<select id="colorSelect" name="Color" tabindex="1" required>
<option value="" selected="selected" disabled>Choose A Color...</option>
<option value="Black">Black</option>
<option value="DarkGrey">Brushed Aluminum</option>
<option value="DarkKhaki">Brass</option>
</select>
</div>
<!--End colorSelection-->
<br>
<!--Begin nameSelection
<div class="left eighty center">Number Of Names
<br>
<input type="radio" name="Names" value="1{p+100}" id="Names_1" class="NameRad1 trigger textBox1" tabindex="5" required />One
<br>
<input type="radio" name="Names" value="2{p+200}" id="Names_2" class="NameRad2 trigger textBox2" tabindex="6" />Two
<br>
<input type="radio" name="Names" value="3{p+300}" id="Names_3" class="NameRad3 trigger textBox3" tabindex="7" />Three
<br>
<input type="radio" name="Names" value="4{p+400}" id="Names_4" class="NameRad4 trigger textBox4" tabindex="8" />Four
<br>
</div>-->
<!--End nameSelection-->
<br>
<!--Begin columnSelection-->
<div class="left eighty center">Number Of Columns
<br>
<input type="radio" name="Columns" value="1" id="Columns_0" class="ColumnRad1 " tabindex="3" required />One
<br>
<input type="radio" name="Columns" value="2" id="Columns_1" class="ColumnRad2 " tabindex="4" />Two
<br>
</div>
<!--End columnSelection-->
<br>
<!--Begin fontSelection-->
<div class="left eighty center">
<label>Font</label>
<br>
<select id="fontSelect" name="Font" required>
<option value="Arial" selected="selected">Modern</option>
<option value="Times New Roman">Classic</option>
<option value="Impact">Vintage</option>
<option value="Verdana">Retro</option>
</select>
</div>
<!--End fontSelection-->
</div>
<!--End leftSide-->
<!--Begin rightSide-->
<div class="submission">
<div class="signCreatorDiv">
<div class="signMain"></div>
<div class="namesColumn">
<div class="NameImg content">
<div class="NameTxt1 right test">
<input class="test" type="text" name="name1" id="textBox1" value="" placeholder="Name 1" tabindex="9" />
</div>
</div>
<div class="NameImg content">
<div class="NameTxt2 right test ">
<input class="test" type="text" name="name2" id="textBox2" value="" placeholder="Name 2" tabindex="10" />
</div>
</div>
<div class="NameImg content">
<div class="NameTxt3 right test">
<input class="test" type="text" name="name3" id="textBox3" value="" placeholder="Name 3" tabindex="11" />
</div>
</div>
<div class="NameImg content">
<div class="NameTxt4 right test">
<input class="test" type="text" name="name4" id="textBox4" value="" placeholder="Name 4" tabindex="12" />
</div>
</div>
</div>
<div class="submitDiv">
<input type="submit" name="x:productsubmit" id="productsubmit" value="Add My Sign" class="submit" tabindex="13" />
</div>
</div>
</div>
<br>
<br>
<!--End rightSide-->
</div>
</form>

fiddle with my (failed) changes to make it a select menu http://jsfiddle.net/EzxAj/2/

So I'd like to be able to turn on and off text areas with a select menu instead of a radio group now. Is there an issue with the syntax I am using, as I can't seem to get anything other than a specific number of divs to show. Does the index not work for select menus in the same way?

Edit:

I also want the select item to default to the third option and have the associated three text fields visible upon page load.

Edit 2

In the actual implementation here: the divs are shown by default, and it looks like the script here:

$('.namesColumn .IH_pINameRow').show();
is causing this, applying a block style to the divs. When I remove it, the styling goes away but the script no longer functions correctly.

Answer №1

Your goal is to retrieve the index of the select menu. However, in order to achieve this, you should focus on obtaining the selected option from the select menu.

var selectedIndex = $(this).find('option:selected').index();

Answer №2

Attila recommends using the following code and making a modification to the last line within the script:

$('.namesColumn .NameImg.content:lt(' + idx + ') input:text').show().prop("required", true);

To set item #3 as the default selection:

$('#fontSelect option:eq(3)').prop('selected', true);
$('#fontSelect').trigger('change');

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

applying conditional rendering in vue.js

I'm currently working on developing a chat application using the guidelines outlined in this tutorial: https://socket.io/get-started/private-messaging-part-1/ One of my goals is to customize the appearance of the messages, so that when a message is s ...

Issue: EMFILE - exceeding maximum number of opened files

I'm currently working with nw.js, attempting to save images in an array of img tags with random names. However, I've encountered a few errors and I'm unsure what's causing them. for (i = 0; i < imgs.length; i++) { request(imgs[i].g ...

Improving User Experience with HTML Form Currency Field: Automatically Formatting Currency to 2 Decimal Places and Setting Maximum Length

Hello there, I am currently facing an issue with the currency auto-formatting in a deposit field on my form. The formatting adds 2 decimal places (for example, when a user types 2500, the field displays 25.00). However, the script I wrote does not seem to ...

Submitting a form using jQuery.serialize over POST method and anticipating a JSON reply from the controller

I'm looking for help on how to send an AJAX request with a serialized form using jQuery and receive a JSON response from the controller. I've tried various solutions without success, as my experience in this area is limited. Could you provide a ...

What is the best way to continuously add items to a div until their heights are equal?

In my layout, I have two divs positioned next to each other. Typically, the left div displays n+2 items while the right div displays n items. The value of n changes depending on the category and is already set. However, there are instances where certain ...

Retrieving all options from a dropdown list in HTML using ASP.net C#

I am currently working with a select list that looks like this: <select name="Section" id="Section" size="5"> <option>Section1</option> <option>Section2</option> <option>Section3</option> <option>Section4< ...

Fixed headers and leftmost column in an HTML spreadsheet ensure optimal navigation and visibility

I need help creating a spreadsheet similar to Google Docs Spreadsheet or Excel, where there is a table full of data. How can I make it so that when a user scrolls vertically, the first row stays fixed, and when they scroll horizontally, the first column st ...

CSS Display Lag

As someone who is just dipping their toes into the world of coding, I've been experimenting with code in my free time. However, I've encountered an issue with a certain code where the CSS seems to have a noticeable delay. Upon initial loading, th ...

Issues with the LIKE operator

I'm currently developing a compact mobile web application. One of the challenges I am facing involves a SQL query that includes a LIKE clause. Here is an example: SELECT from posts WHERE (title LIKE '%car%') or (content LIKE '%car%&apo ...

The properties of a JSON object are not explicitly defined

When I make an AJAX call, I receive a JSON object and log it using this code: console.log(response); This is the response that gets logged in the console: {"filename":"new.jpg","orientation":"vertical"} However, when I try to access the orientation pro ...

Generating images using Node.js and GraphicsMagick

I'm looking for a way to generate an image using graphicsMagick and node.js. Typically, I can achieve this with the following command: gm convert -background transparent -pointsize 30 -gravity Center label:türkçee HEEEEEY.png But I need to replic ...

Ensuring Uniform Height for Content within a "Row"

Currently, I am working on resolving a bug in this website. In order to pinpoint the issue, I have replicated it in the sandbox provided below: https://codesandbox.io/s/confident-engelbart-q70uc8?file=/index.html The problem lies in the fact that wheneve ...

AngularJS and adding to an array in the routing process

I'm currently working on creating a contact list with two different views. One view displays all the contacts and includes an option to add a new contact, which is represented by a button rather than a space to input information directly. The other vi ...

changing the contents of an array within the current state

My current task involves updating a list of names in the state before sending it as props to another component. // code snippet omitted for brevity this.state = { // other states here playerName: { player1Name: 'Default Player 1 Name ...

What is the best way to adjust the canvas size within a list item?

I have successfully created a 3D model with Zdog that dynamically resizes based on the screen size. The model includes a dome and brim, which rotate smoothly as intended. Here is the code snippet: const TAU = Zdog.TAU; let hat = new Zdog.Illustration({ ...

Clicking on the "Select All" option in the angular2-multiselect-dropdown triggers the onDeSelectAll event

I'm facing an issue with angular2-multiselect-dropdown where the "SelectAll" functionality is not working correctly. When I click on "SelectAll", it also triggers the deSelectAll event, causing the onDeSelectAll() function to be called and preventing ...

Encountering issues with React Apollo hooks after integrating React Native into the monorepo system

I am in the process of setting up a React web app and a React-native app within a monorepo using yarn workspaces. The web and controllers are functioning properly, allowing me to successfully execute graphql queries to my apollo-express server. However, up ...

Specifying a zero margin on a Bootstrap container does not remove the margin

I am trying to make my right container elements fill the entire width of their column. After inspecting in Chrome, I noticed that my bootstrap container had default margins on both sides. To fix this, I set the margins to zero. Although the left margin di ...

The object does not recognize the property or method 'dataTable'; even when jQuery is included

I have been troubleshooting this issue within the realm of jQuery and data tables, but none of the recommended solutions have resolved it (in case this appears to be a duplicate right away!) Within my .Net project (C#), I have references to jQuery, Datata ...

Why does using rgba color with an alpha value cause my div to become see-through?

I am currently attempting to assign a background color to a specific style of pop-up, but whenever I use a value that includes an alpha channel, it ends up being transparent. This is the CSS code I have been using: --color: 255, 144, 106, 0.18; background ...