How can I make the jquery run properly when loading the page?

0

It turns out that I have a divs filtering code, which shows me one or another element, the filtering I do with jQuery, the problem is that I want to start loading the page, do not show me all the options, if not, do not show me any and when selecting one of the options show by correct filtering

The code is the following:

$(document).ready()
		$('#tipoImpresora').change(function () {
	    var year = $(this).val();
	    $("div").hide();
	    $("div:contains('" + year + "')").show();
		});
div {
  background: #eee;
  height: 300px;
  width: 280px;
  text-align: center;
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
		<select id="tipoImpresora">
			<option value="0">ninguno</option>
			<option value="Impresora Laser" selected="true">laser</option>
			<option value="Impresora Tinta">tinta</option>
		</select>
	</form>

	  	<div class="Laser" style="background-color: red;">
			<p>Impresora Laser</p>
			<img src="#">
			<img src="#">
			<br>
			<table>
				<tr>
					<td>Marca</td>
					<td><input type="text" name="txtMarca"></td>
				</tr>
				<tr>
					<td>Modelo</td>
					<td><input type="text" name="txtMarca"></td>
				</tr>
				<tr>
					<td>Color Tinta</td>
					<td><input type="text" name="txtMarca"></td>
				</tr>
			</table>
			<br>
			<button>Seleccionar</button>
		</div>
		<div class="Tinta" style="background-color: blue;">
			<p>Impresora Tinta</p>
			<img src="#">
			<img src="#">
			<br>
			<table>
				<tr>
					<td>Marca</td>
					<td><input type="text" name="txtMarca"></td>
				</tr>
				<tr>
					<td>Modelo</td>
					<td><input type="text" name="txtMarca"></td>
				</tr>
				<tr>
					<td>Color Tinta</td>
					<td><input type="text" name="txtMarca"></td>
				</tr>
			</table>
			<br>
			<button>Seleccionar</button>
		</div>

It turns out that when loading the page it shows me the red block and the blue block, and I do not want to show any of them except that I make my first choice in the select.

    
asked by Ivan Arenas 24.05.2018 в 09:29
source

3 answers

2

It will always show you the div if you do not hide it first with:

style="display:none"
    
answered by 24.05.2018 / 09:39
source
2

As you comment @ lois6b, the $(document).ready(function(){...}) is misspelled. Also, the ideal would be to use $(function(){...});

On the other hand, to hide the divs initially you can simply use:

 $("div.Laser").hide();
 $("div.Tinta").hide();

You should also put the option "none" as selected since it makes more sense if from the beginning you want to hide the two divs:

$(function(){
        $("div.Laser").hide();
        $("div.Tinta").hide();
        
    		$('#tipoImpresora').change(function () {
    	    var year = $(this).val();
    	    $("div").hide();
    	    $("div:contains('" + year + "')").show();
    		});
        
});
div {
      background: #eee;
      height: 300px;
      width: 280px;
      text-align: center;
      display: inline-block;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
    		<select id="tipoImpresora">
    			<option value="0" selected="true">ninguno</option>
    			<option value="Impresora Laser">laser</option>
    			<option value="Impresora Tinta">tinta</option>
    		</select>
    	</form>

    	  	<div class="Laser" style="background-color: red;">
    			<p>Impresora Laser</p>
    			<img src="#">
    			<img src="#">
    			<br>
    			<table>
    				<tr>
    					<td>Marca</td>
    					<td><input type="text" name="txtMarca"></td>
    				</tr>
    				<tr>
    					<td>Modelo</td>
    					<td><input type="text" name="txtMarca"></td>
    				</tr>
    				<tr>
    					<td>Color Tinta</td>
    					<td><input type="text" name="txtMarca"></td>
    				</tr>
    			</table>
    			<br>
    			<button>Seleccionar</button>
    		</div>
    		<div class="Tinta" style="background-color: blue;">
    			<p>Impresora Tinta</p>
    			<img src="#">
    			<img src="#">
    			<br>
    			<table>
    				<tr>
    					<td>Marca</td>
    					<td><input type="text" name="txtMarca"></td>
    				</tr>
    				<tr>
    					<td>Modelo</td>
    					<td><input type="text" name="txtMarca"></td>
    				</tr>
    				<tr>
    					<td>Color Tinta</td>
    					<td><input type="text" name="txtMarca"></td>
    				</tr>
    			</table>
    			<br>
    			<button>Seleccionar</button>
    		</div>
        </form>
    
answered by 24.05.2018 в 10:04
0

You can hide the default divs with CSS by applying display: none .

Another option would be to fire the event change to the element select to first establish the value you want in the following way:

$('#tipoImpresora')
    .val('0')
    .trigger('change');
    
answered by 24.05.2018 в 09:35