How can I make this JQuery and HTML, per-row specific? - html

In CB UserList the form appears multiple times (one for every record) and the below code is expected to produce the submit button and the modules to show/hide for every record that appears on the list separately. When a user makes a selection on the first row, everything works fine but only for the first row. Button and module divs show/hide properly on the first row (first record) only. The problem is that as the code is now, the selection on the first row is applied to all rows and the show/hide appears to all rows simultaneously. I need it to appear in each row separately, like when I select the first or any row, the other rows to remain unchanged unless I select them. Maybe I need something like parent/child/siblings relation for the button and the modules?
My html is as follows
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<select name="test" class="form-control">
<option label="Please select" selected="selected" value="">Please select</option>
<optgroup label="test1" style="font-weight: bolder;">
<option label="1a" value="UserSubmit, free"></option>
<option label="1b" value="6"></option>
<option label="1c" value="3"></option>
</optgroup>
<optgroup label="test2" style="font-weight: bolder;">
<option label="2a" value="O24"></option>
<option label="2b" value="O12"></option>
<option label="2c" value="O6"></option>
<option label="2d" value="O2"></option>
</optgroup>
</select>
</div>
<div id="free">{loadmoduleid 240}</div>
<div id="button"><input name="submit" type="submit" value="somevalue" id="submit-button" class="btn btn-primary UserSubmit" /></div>
<div id="6">{loadmoduleid 234}</div>
<div id="3">{loadmoduleid 235}</div>
<div id="O24">{loadmoduleid 236}</div>
<div id="O12">{loadmoduleid 237}</div>
<div id="O6">{loadmoduleid 238}</div>
<div id="O2">{loadmoduleid 239}</div>
</div>
</div>
My JQuery
$(document).ready(function () {
$("select").on('change', function () {
var x = this.selectedIndex;
if (x == "1") {
$(".UserSubmit").show();
$("#free").show();
$("#6").hide();
$("#3").hide();
$("#O24").hide();
$("#O12").hide();
$("#O6").hide();
$("#O2").hide();
} else if (x == "2") {
$(".UserSubmit").hide();
$("#free").hide();
$("#6").show();
$("#3").hide();
$("#O24").hide();
$("#O12").hide();
$("#O6").hide();
$("#O2").hide();
} else if (x == "3") {
more ifs until x == "7"
} else {
$(".UserSubmit").hide();
$("#free").hide();
$("#6").hide();
$("#3").hide();
$("#O24").hide();
$("#O12").hide();
$("#O6").hide();
$("#O2").hide();
}
});
$(".UserSubmit").css("display", "none");
$("#free").css("display", "none");
$("#6").css("display", "none");
$("#3").css("display", "none");
$("#O24").css("display", "none");
$("#O12").css("display", "none");
$("#O6").css("display", "none");
$("#O2").css("display", "none");
});

Okay, I scribbled something, I hope I understood correctly and this code reflects what you wanted.
const selectElement = document.querySelectorAll('select');
selectElement.forEach(select => {
const colElement = select.parentNode.closest('.col-md-6');
const withoutFormGroup = (Array.from(colElement.children).filter(child => child.id !== select.id));
// hidden all elements
withoutFormGroup.forEach(child => {
child.style.display = 'none';
});
// show selected element
select.addEventListener('change', function() {
const indexSelected = this.selectedIndex;
const indexValue = this.value;
// show selected element
withoutFormGroup.forEach((child, index) => {
child.style.display = child.id === indexValue ? 'block' : 'none';
// if selected element is 1 then show
// first and second element
if (indexSelected === 1 && (index === 0 || index === 1)) {
child.style.display = 'block';
}
});
});
});
.container {
display: inline-block;
padding: 20px;
border: 1px solid red;
}
.col-md-6>*+* {
margin-top: 10px;
}
.row+.row {
margin-top: 20px;
}
.active {
background-color: #000;
color: #fff;
}
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<select name="test" class="form-control">
<option label="Please select" selected="selected" value="">Please select</option>
<optgroup label="test1" style="font-weight: bolder;">
<option label="1a" value="UserSubmit, free"></option>
<option label="1b" value="6"></option>
<option label="1c" value="3"></option>
</optgroup>
<optgroup label="test2" style="font-weight: bolder;">
<option label="2a" value="O24"></option>
<option label="2b" value="O12"></option>
<option label="2c" value="O6"></option>
<option label="2d" value="O2"></option>
</optgroup>
</select>
</div>
<div id="free">{loadmoduleid 240}</div>
<div id="button">
<input name="submit" type="submit" value="somevalue" id="submit-button" class="btn btn-primary UserSubmit" />
</div>
<div id="6">{loadmoduleid 234}</div>
<div id="3">{loadmoduleid 235}</div>
<div id="O24">{loadmoduleid 236}</div>
<div id="O12">{loadmoduleid 237}</div>
<div id="O6">{loadmoduleid 238}</div>
<div id="O2">{loadmoduleid 239}</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<select name="test" class="form-control">
<option label="Please select" selected="selected" value="">Please select</option>
<optgroup label="test1" style="font-weight: bolder;">
<option label="1a" value="UserSubmit, free"></option>
<option label="1b" value="6"></option>
<option label="1c" value="3"></option>
</optgroup>
<optgroup label="test2" style="font-weight: bolder;">
<option label="2a" value="O24"></option>
<option label="2b" value="O12"></option>
<option label="2c" value="O6"></option>
<option label="2d" value="O2"></option>
</optgroup>
</select>
</div>
<div id="free">{loadmoduleid 240}</div>
<div id="button">
<input name="submit" type="submit" value="somevalue" id="submit-button" class="btn btn-primary UserSubmit" />
</div>
<div id="6">{loadmoduleid 234}</div>
<div id="3">{loadmoduleid 235}</div>
<div id="O24">{loadmoduleid 236}</div>
<div id="O12">{loadmoduleid 237}</div>
<div id="O6">{loadmoduleid 238}</div>
<div id="O2">{loadmoduleid 239}</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<select name="test" class="form-control">
<option label="Please select" selected="selected" value="">Please select</option>
<optgroup label="test1" style="font-weight: bolder;">
<option label="1a" value="UserSubmit, free"></option>
<option label="1b" value="6"></option>
<option label="1c" value="3"></option>
</optgroup>
<optgroup label="test2" style="font-weight: bolder;">
<option label="2a" value="O24"></option>
<option label="2b" value="O12"></option>
<option label="2c" value="O6"></option>
<option label="2d" value="O2"></option>
</optgroup>
</select>
</div>
<div id="free">{loadmoduleid 240}</div>
<div id="button">
<input name="submit" type="submit" value="somevalue" id="submit-button" class="btn btn-primary UserSubmit" />
</div>
<div id="6">{loadmoduleid 234}</div>
<div id="3">{loadmoduleid 235}</div>
<div id="O24">{loadmoduleid 236}</div>
<div id="O12">{loadmoduleid 237}</div>
<div id="O6">{loadmoduleid 238}</div>
<div id="O2">{loadmoduleid 239}</div>
</div>
</div>
</div>
It's best if you put this code from id="free" to id="02" in an additional div with some class because currently I rely on the "col-md-6" class and it can be disastrous ;)
And most importantly, I haven't been using jquery for a few years, so the code is in plain js ;)
I can see that you are relying on the id, remember that there can only be one id the same on a page. You cannot duplicate them. It's best to use data attributes.
EDIT
In one of the threads you wrote "... list of hundreds or thousands of users ..." And you did not mention that in the description;). I modified the solution and hide the data using css, not js. If we do not do this, before js starts working, all elements that should not be visible will be visible on the website.
const selectElement = document.querySelectorAll('select');
selectElement.forEach(select => {
const modules = select.parentNode.nextElementSibling;
// show selected element
select.addEventListener('change', function() {
const indexSelected = this.selectedIndex;
const indexValue = this.value;
// show selected element
[].slice.call(modules.children).forEach((child, index) => {
child.style.display = child.dataset.id === indexValue ? 'block' : 'none';
// if selected element is 1 then show
// first and second element
if (indexSelected === 1 && (index === 0 || index === 1)) {
child.style.display = 'block';
}
});
});
});
.container {
display: flex;
padding: 20px;
align-items: top;
border: 1px solid red;
gap: 30px;
}
.form-group {
margin-bottom: 10px;
}
.modules>* {
display: none;
}
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<select name="test" class="form-control">
<option label="Please select" selected="selected" value="">Please select</option>
<optgroup label="test1" style="font-weight: bolder;">
<option label="1a" value="UserSubmit, free"></option>
<option label="1b" value="6"></option>
<option label="1c" value="3"></option>
</optgroup>
<optgroup label="test2" style="font-weight: bolder;">
<option label="2a" value="O24"></option>
<option label="2b" value="O12"></option>
<option label="2c" value="O6"></option>
<option label="2d" value="O2"></option>
</optgroup>
</select>
</div>
<div class="modules">
<div data-id="free">{loadmoduleid 240}</div>
<div data-id="button">
<input name="submit" type="submit" value="somevalue" id="submit-button" class="btn btn-primary UserSubmit" />
</div>
<div data-id="6">{loadmoduleid 234}</div>
<div data-id="3">{loadmoduleid 235}</div>
<div data-id="O24">{loadmoduleid 236}</div>
<div data-id="O12">{loadmoduleid 237}</div>
<div data-id="O6">{loadmoduleid 238}</div>
<div data-id="O2">{loadmoduleid 239}</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<select name="test" class="form-control">
<option label="Please select" selected="selected" value="">Please select</option>
<optgroup label="test1" style="font-weight: bolder;">
<option label="1a" value="UserSubmit, free"></option>
<option label="1b" value="6"></option>
<option label="1c" value="3"></option>
</optgroup>
<optgroup label="test2" style="font-weight: bolder;">
<option label="2a" value="O24"></option>
<option label="2b" value="O12"></option>
<option label="2c" value="O6"></option>
<option label="2d" value="O2"></option>
</optgroup>
</select>
</div>
<div class="modules">
<div data-id="free">{loadmoduleid 240}</div>
<div data-id="button">
<input name="submit" type="submit" value="somevalue" id="submit-button" class="btn btn-primary UserSubmit" />
</div>
<div data-id="6">{loadmoduleid 234}</div>
<div data-id="3">{loadmoduleid 235}</div>
<div data-id="O24">{loadmoduleid 236}</div>
<div data-id="O12">{loadmoduleid 237}</div>
<div data-id="O6">{loadmoduleid 238}</div>
<div data-id="O2">{loadmoduleid 239}</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<select name="test" class="form-control">
<option label="Please select" selected="selected" value="">Please select</option>
<optgroup label="test1" style="font-weight: bolder;">
<option label="1a" value="UserSubmit, free"></option>
<option label="1b" value="6"></option>
<option label="1c" value="3"></option>
</optgroup>
<optgroup label="test2" style="font-weight: bolder;">
<option label="2a" value="O24"></option>
<option label="2b" value="O12"></option>
<option label="2c" value="O6"></option>
<option label="2d" value="O2"></option>
</optgroup>
</select>
</div>
<div class="modules">
<div data-id="free">{loadmoduleid 240}</div>
<div data-id="button">
<input name="submit" type="submit" value="somevalue" id="submit-button" class="btn btn-primary UserSubmit" />
</div>
<div data-id="6">{loadmoduleid 234}</div>
<div data-id="3">{loadmoduleid 235}</div>
<div data-id="O24">{loadmoduleid 236}</div>
<div data-id="O12">{loadmoduleid 237}</div>
<div data-id="O6">{loadmoduleid 238}</div>
<div data-id="O2">{loadmoduleid 239}</div>
</div>
</div>
</div>
</div>
I added a modules class in which I hide elements using css. Well, these elements have a data-id not an id. It should be correct now.

You can use the toggle() method on the div whose id matches the selected option.
$(document).ready(function () {
$("select").on('change', function () {
var target = $('option:selected', this).attr('data-toggle-target');
$('#' + target).toggle();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<select name="test" class="form-control">
<option label="Please select" selected="selected" value="">Please select</option>
<optgroup label="test1" style="font-weight: bolder;">
<option label="1a" value="value1a" data-toggle-target="6"></option>
<option label="1b" value="value1b" data-toggle-target="3"></option>
<option label="1c" value="value1c" data-toggle-target="024"></option>
</optgroup>
<optgroup label="test2" style="font-weight: bolder;">
<option label="2a" value="value2a" data-toggle-target="012"></option>
<option label="2b" value="value2b" data-toggle-target="06"></option>
<option label="2c" value="value2c" data-toggle-target="02"></option>
</optgroup>
</select>
</div>
<div id="free">{loadmoduleid 240}</div>
<div id="button"><input name="submit" type="submit" value="somevalue" id="submit-button" class="btn btn-primary UserSubmit" /></div>
<div id="6">{loadmoduleid 234}</div>
<div id="3">{loadmoduleid 235}</div>
<div id="O24">{loadmoduleid 236}</div>
<div id="O12">{loadmoduleid 237}</div>
<div id="O6">{loadmoduleid 238}</div>
<div id="O2">{loadmoduleid 239}</div>
</div>
</div>
And if you don't need the value attribut on the options anywhere else, I suggest you replace the value of each option with the corresponding id's of the divs and you can simplify you jQuery to this:
$(document).ready(function () {
$("select").on('change', function () {
var target = $(this).val();
$('#' + target).toggle();
});
});
Sadly, this won't work if you hit the same option twice in a row, since the <select> selected item doesn't change. You could use click event on the <select>, but that fires twice (first when you open the dropdown, second when selecting the value) so you would need to filter out the first event. click event on <option> doesn't work, at least on Chrome.

Related

Show & hide html element based on selected text using jquery

I want to set attribute name & show select option if the selected text is not "Super Admin" and unset the attribute name & hide the select option if the selected text is "Super Admin".
<div class="form-group">
<select id="role_user" name="role" class="form-control" required>
<option value="">Select role user</option>
<option value="Admin">Admin</option>
<option value="Supervisor">Supervisor</option>
<option value="Super Admin">Super Admin</option>
</select>
</div>
The element that I want to show & hide:
<div class="form-group" id="company" style="display: none">
<label>Company</label>
<select id="company_i" class="form-control">
<option value="">Select Company</option>
#foreach ($company as $cp)
<option value="{{ $cp->id }}">{{ $cp->name }}</option>
#endforeach
</select>
</div>
JQuery code:
$(document).ready(function() {
$('#role_user').change(function() {
if ($('#role_user option:selected').text() != "Super Admin") {
$('#company').show();
$('#company_i').attr('name', 'company_id');
} else if ($('#role_user option:selected').text() == "Super Admin") {
$('#company').hide();
$('#company_i').removeAttr("name");
}
})
});
I have tried it using the code above, but only the set & remove name attribute are working. The show & hide not working.
$(document).ready(function () {
$('#role_user').change(function () {
if ($(this).val() != "Super Admin") {
$('#company').show();
$('#company_i').attr('name', 'company_id');
} else {
$('#company').hide();
$('#company_i').removeAttr("name");
}
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
<select id="role_user" name="role" class="form-control" required>
<option value="">Select role user</option>
<option value="Admin">Admin</option>
<option value="Supervisor">Supervisor</option>
<option value="Super Admin">Super Admin</option>
</select>
</div>
<div class="form-group" id="company" style="display: none">
<label>Company</label>
<select id="company_i" class="form-control">
<option value="">Select Company</option>
</select>
</div>

How to access another child class from parent in JQuery?

I have my markup looks like:
<div class="col-md-12 mainDiv">
<div class="rowDiv"> <!--first row starts-->
<div class="a1">
<div class="form-group">
<div class="dropdown-container-skills">
<select class="dropdown-skills">
<option>Select Any</option>
<option value="S1">Skill 1</option>
<option value="S2">Skill 2</option>
<option value="S3">Skill 3</option>
</select>
</div>
</div>
</div>
<div class="a1">
<div class="form-group">
<div class="dropdown-container-topics">
<select class="dropdown-topics">
<option>Select Any</option>
<option value="T1">Topic 1</option>
<option value="T2">Topic 2</option>
<option value="T3">Topic 3</option>
</select>
</div>
</div>
</div>
</div><!--first row ends-->
<div class="rowDiv"> <!--second row starts-->
<div class="a1">
<div class="form-group">
<div class="dropdown-container-skills">
<select class="dropdown-skills">
<option>Select Any</option>
<option value="S1">Skill 1</option>
<option value="S2">Skill 2</option>
<option value="S3">Skill 3</option>
</select>
</div>
</div>
</div>
<div class="a1">
<div class="form-group">
<div class="dropdown-container-topics">
<select class="dropdown-topics">
<option>Select Any</option>
<option value="T1">Topic 1</option>
<option value="T2">Topic 2</option>
<option value="T3">Topic 3</option>
</select>
</div>
</div>
</div>
</div><!--second row ends-->
</div>
I want something like when I select skill from first row it will update topics in the first row itself. Also when I select skill from second row it will update topics in second row and so on. I wrote the script for this as:
$('body').on('change', '.dropdown-skills', function(e) {
$.ajax({
type: "GET",
url: 'getTopics/' + $(this).val(),
dataType : "html",
success: function(response)
{
$(this).closest('.rowDiv').find('.dropdown-container-topics').html(response);
}
});
});
ie when I change class dropdown-skills it will get the topics as html from the controller function and it is then fed into class dropdown-container-topics of the current row.
But this is not working. How can it be done?

Dropdown is not showing after a page reloads

I have three dropdowns in my page, two of them are shown perfectly while the other one is not appearing, and when looking at the element in page it gets loaded with the "style="display: none;" value even if I'm forcing it with style="display: inline;".
Here's the code:
<div class="dropdown">
<div id="myBrokenDropdown" class="dropdown-content">
<select class='filter' id="notWorkingOne">
<option value=""> Select</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
</div>
<!--select button-->
<div id="select1" class="dropdown-content">
<select class='filter' id="select1Works" data-col="1">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
</div>
<div id="select2" class="dropdown-content">
<select class='filter' id="select2Works" data-col="5">
<option value=""> Select</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
</div>
</div>
Also, in jquery I can hide/Show the two dropdowns with the following code:
$('#select1Works').hide();
$('#select2Works').hide();
$('#notWorkingOne').hide();
$(document).ready(function() {
$("input[type='radio']").on('change', function() {
$("#select1Works").toggle($("[name=radioButton]").index(this) === 0);
$("#select2Works").val("").trigger("change");
$("#select2Works").toggle($("[name=radioButton]").index(this) === 1);
$("#select1Works").val("").trigger("change");
if ($('#select1Works').is(':visible') || $('#select2Works').is(':visible')) {
$('#notWorkingOne').show();
}
})
});
Why is the "myBrokenDropdown" not showing?
Thank you very much
It was an issue with a selector in my actual code, I missed a '#'.

How to make textarea same height as select while still being reponsive?

I have a form that I am developing and while I can get the form fields to be responsive for width, the textarea box is slightly taller than the select field to the right. How can I make these two the same height while keeping both responsive for mobile?
Here is a codepen of the form. Below is the code:
<div class="row">
<div class="col-md-6">
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label for="00NW0000001Zjiv" class="control-label" style="font-weight:300">Truck Model(s)</label>
<textarea id="00NW0000001Zjiv" name="00NW0000001Zjiv" type="text" class="form-control input-lg" rows="5"></textarea><span class="help-block bg-danger"></span>
</div>
</div>
</div>
<div class="col-md-6">
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label for="00NW0000001Zjje" class="control-label" style="font-weight:300">State(s) in which the truck(s) run</label>
<select id="00NW0000001Zjje" name="00NW0000001Zjje" multiple="multiple" class="form-control input-lg">
<option value="Alabama">Alabama</option>
<option value="Alaska">Alaska</option>
<option value="Arizona">Arizona</option>
<option value="Arkansas">Arkansas</option>
<option value="California">California</option>
<option value="Colorado">Colorado</option>
<option value="Connecticut">Connecticut</option>
<option value="Delaware">Delaware</option>
<option value="Florida">Florida</option>
<option value="Georgia">Georgia</option>
<option value="Hawaii">Hawaii</option>
<option value="Idaho">Idaho</option>
<option value="Illinois">Illinois</option>
<option value="Indiana">Indiana</option>
<option value="Iowa">Iowa</option>
<option value="Kansas">Kansas</option>
<option value="Kentucky">Kentucky</option>
<option value="Louisiana">Louisiana</option>
<option value="Maine">Maine</option>
<option value="Maryland">Maryland</option>
<option value="Massachusetts">Massachusetts</option>
<option value="Michigan">Michigan</option>
<option value="Minnesota">Minnesota</option>
<option value="Mississippi">Mississippi</option>
<option value="Missouri">Missouri</option>
<option value="Montana">Montana</option>
<option value="Nebraska">Nebraska</option>
<option value="Nevada">Nevada</option>
<option value="New Hampshire">New Hampshire</option>
<option value="New Jersey">New Jersey</option>
<option value="New Mexico">New Mexico</option>
<option value="New York">New York</option>
<option value="North Carolina">North Carolina</option>
<option value="North Dakota">North Dakota</option>
<option value="Ohio">Ohio</option>
<option value="Oklahoma">Oklahoma</option>
<option value="Oregon">Oregon</option>
<option value="Pennsylvania">Pennsylvania</option>
<option value="Rhode Island">Rhode Island</option>
<option value="South Carolina">South Carolina</option>
<option value="South Dakota">South Dakota</option>
<option value="Tennessee">Tennessee</option>
<option value="Texas">Texas</option>
<option value="Utah">Utah</option>
<option value="Vermont">Vermont</option>
<option value="Virginia">Virginia</option>
<option value="Washington">Washington</option>
<option value="West Virginia">West Virginia</option>
<option value="Wisconsin">Wisconsin</option>
<option value="Wyoming">Wyoming</option>
</select><span class="help-block bg-danger"></span>
</div>
</div>
</div>
</div>
Thanks!
It is possible by giving manually height to textarea. So give height to textarea on media queries.
In this way you would be able to achieve the responsiveness as well.
I hope this might help you
JSFiddle Link
<div class="row">
<div class="left">
<label for="first-name">First Name</label>
<input type="text" name="first-name" placeholder="First Name" id="first-name" required="required" />
</div>
<div class="right">
<label for="details">Details</label>
<textarea name="details" placeholder="Details" id="details" required></textarea>
</div>
</div>
CSS:
#media only screen and (min-width: 600px) {
textarea { height: calc(100% - 0.75em); /* 100% fill height, minus height of details label */ }
}

How to place to divs with drop down lists in them next to each other?

<div style="width: 100%; overflow: hidden;">
<div style="margin-left: 100px; width: 600px; float: left;">
<div class="description">
<h2 style="color:blue">Select the appropriate version</h2>
</div>
<select>
<option value="" disabled selected>Select version</option>
<option value="" disabled selected>Select feature</option>
<option value="A"> A </option>
<option value="B"> B </option>
<option value="C"> C</option>
</select>
<div style=" margin-left: 350px; width: 600px; float: left;">
<div class="description">
<h2 style="color:blue">Select the appropriate category</h2>
</div>
<select>
<option value="" disabled selected>Select feature</option>
<option value="" disabled selected>Select category</option>
<option value="A"> A </option>
<option value="B"> B </option>
<option value="C"> C</option>
</select>
<div style="margin-left: 620px; width: 600px; float: left;">
<div class="description">
<br>
<h2 style="color:blue">Select the feature</h2>
</div>
<select>
<option value="" disabled selected>Select feature</option>
<option value="" disabled selected>Select category</option>
<option value="A"> A </option>
<option value="B"> B </option>
<option value="C"> C</option>
</select>
</div>
</div>
I want the three divs with the drop down lists to be next to each other horizontally. I have tried using float, inline and inline block. How can it be done in HTML.
Use inline-block and put properly your all divs.
Here is an example:
.selection {
display: inline-block;
}
h1{
display: inline-block;
font-size: 14px;
}
<div>
<div class="selection">
<h1>Select Option</h1>
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</div>
<div class="selection">
<h1>Select Option</h1>
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</div>
<div class="selection">
<h1>Select Option</h1>
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</div>
</div>
<div style="float:left">
dropdown1
</div>
<div style="float:left">
dropdown2
</div>
<div style="float:left">
dropdown3
</div>
This will definitely work.
Problem in your code is over width. Better is you give width into % instead of px.
First of all, your code is really badly formatted.
Your are using 7 Div but you are missing 2 Div closing tags. Close the Div tags properly.
You are also fixing the width of all 3 divisions, so if the screen size is smaller than the combined width of the 3 div, your div's will no longer be displayed horizontally. Give width relative like width:30%.
Also remove margin from the elements. Use Padding instead to give space between div's.
<html>
<body>
<div style="width: 100%; overflow: hidden;">
<div style=" width: 33%;float: left;">
<div class="description">
<h2 style="color:blue">Select the appropriate version</h2>
</div>
<select>
<option value="" disabled selected>Select version</option>
<option value="" disabled selected>Select feature</option>
<option value="A"> A </option>
<option value="B"> B </option>
<option value="C"> C</option>
</select>
</div>
<div style="float: left;width: 33%;">
<div class="description">
<h2 style="color:blue">Select the appropriate category</h2>
</div>
<select>
<option value="" disabled selected>Select feature</option>
<option value="" disabled selected>Select category</option>
<option value="A"> A </option>
<option value="B"> B </option>
<option value="C"> C</option>
</select>
</div>
<div style=" float: left;width: 33%;">
<div class="description">
<br>
<h2 style="color:blue">Select the feature</h2>
</div>
<select>
<option value="" disabled selected>Select feature</option>
<option value="" disabled selected>Select category</option>
<option value="A"> A </option>
<option value="B"> B </option>
<option value="C"> C</option>
</select>
</div>
</div>
</body>
</html>
Problem is with your code you have 3 unclosed div tags and a lot of left margin plus try to use width and margin both in %
[Check pen] (http://codepen.io/anon/pen/gPzJpr)
for your html
<div class="container">
<div id="firstDiv">
<h1>Select the appropriate version</h1>
<select class="" name="">
<option value="" disabled selected>Select version</option>
<option value="" disabled selected>Select feature</option>
<option value="A"> A </option>
<option value="B"> B </option>
<option value="C"> C</option>
</select>
</div>
<div id="secondDiv">
<h1>Select the appropriate version</h1>
<select class="" name="">
<option value="" disabled selected>Select version</option>
<option value="" disabled selected>Select feature</option>
<option value="A"> A </option>
<option value="B"> B </option>
<option value="C"> C</option>
</select>
</div>
<div id="thirdDiv">
<h1>Select the appropriate version</h1>
<select class="" name="">
<option value="" disabled selected>Select version</option>
<option value="" disabled selected>Select feature</option>
<option value="A"> A </option>
<option value="B"> B </option>
<option value="C"> C</option>
</select>
</div>
</div>
for your css:
div{
display: inline-block;
position: relative;
margin: 5px;
padding: 15px;
}
#firstDiv{
background-color: #ccc;
}
#secondDiv{
background-color: #eee;
}
#thirdDiv{
background-color: #aaa;
}
Just like what #Maihan Nijat said, use external CSS to make it easier for you to debug.
I made a few changes to your code. The absolute easiest way to solve this problem is to ensure that the width of each element could fit next to each other on the screen. Not only does this require that the text is not too large, but also that the width you explicitly define does not overflow.
The simple solution to your problem is to place display: inline-block; on the elements which you would like side by side.
.left {
display: inline-block;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<div class="left">
<div class="description">
<h2 style="color:blue">Select the appropriate version</h2>
</div>
<select>
<option value="" disabled selected>Select version</option>
<option value="" disabled selected>Select feature</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
<div class="left">
<div class="description">
<h2 style="color:blue">Select the appropriate category</h2>
</div>
<select>
<option value="" disabled selected>Select feature</option>
<option value="" disabled selected>Select category</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
<div class="left">
<div class="description">
<br>
<h2 style="color:blue">Select the feature</h2>
</div>
<select>
<option value="" disabled selected>Select feature</option>
<option value="" disabled selected>Select category</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
</body>
</html>