I'm having a little trouble with jQuery, I wish to have 3 separate select boxes, and when a select box is chosen the parent div then gets given that selected Option text as a class. I am having issues however as it instead displays all of the values on the page instead of the individual one.
I have tried using (this) but I had no luck with it unfortunately.
My aim is to click the middle box for example and change the option to 'Blue', so then it's parent div is
<div class="tableoptioncontainer Blue "</div>
but instead the problem is this
<div class="tableoptioncontainer ColourBlueColour"</div>
https://jsfiddle.net/jack319082/psLtarno/6/
HTML:
<div class="row-container">
<div class="tableoptioncontainer">
<select id="selectbox">
<option>Colour</option>
<option>Red</option>
<option>Orange</option>
<option>Light Orange</option>
<option>Yellow</option>
</select>
</div>
<div class="tableoptioncontainer">
<select id="selectbox">
<option>Colour</option>
<option>Light Blue</option>
<option>Blue</option>
<option>Dark Blue</option>
<option>Violet</option>
</select>
</div>
<div class="tableoptioncontainer">
<select id="selectbox">
<option>Colour</option>
<option>Dark Green</option>
<option>Moss</option>
<option>Green</option>
<option>Aqua</option>
</select>
</div>
</div>
CSS:
.row-container {
background-color: grey;
padding: 10px 20px;
display: inline-block;
}
.tableoptioncontainer {
padding: 12px;
margin: 8px 0px;
background-color: rgba(0, 0, 0, 0.2);
}
/* Background Colours for when it works */
.Red {background-color: #ff0000}
.Orange {background-color: #ff9000}
.Light-Orange {background-color: #ffc272}
.Yellow {background-color: #fff225}
.Light-Blue {background-color: #8bc2ff}
.Blue {background-color: #0078ff}
.Dark-Blue {background-color: #0030ba}
.Violet {background-color: #4603dd}
.Dark-Green {background-color: #00390f}
.Moss {background-color: #009126}
.Green {background-color: #00f13e}
.Aqua {background-color: #00f1d2}
jQuery:
jQuery(document).ready(function(){
jQuery("select").change(function(){
var colourName = jQuery('select#selectbox :selected').text();
colourName = colourName.replace(/ /g, '-');
jQuery(this).parent().attr('class','tableoptioncontainer');
jQuery(this).parent().addClass(colourName);
});
});
You have to change your script like this
jQuery(document).ready(function(){
jQuery("select").change(function(){
var colourName = jQuery(this).find('option:selected').text();
colourName = colourName.replace(/ /g, '-');
jQuery(this).parent().attr('class','tableoptioncontainer');
jQuery(this).parent().addClass(colourName);
});
});
Your declaration var colourName = jQuery('select#selectbox:selected').text();
selects the text of the selected options from all 3 selectboxes. And note that ids have to be unique, you have the same id="selectbox"
for all selects.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.