Ad

Get Value From Input Field With JQuery

- 1 answer

i would like to get the value from an input field. The code looks like this:

<input type="number" step="any" 
name="properties[wallpaper_size_width]" class="spb-productdescfont 
spb-productoptiontextcolor spb-productoptionbackground" 
placeholder="Breite in cm" required="">

Because I dont have an id for that input

$("#height").value(); 

will not work. I confused by the [] @name=

How can I select the value by the name?

Ad

Answer

You can use the name selector like this,

$('.getVal').click(function(){
   var value = $("[name='properties[wallpaper_size_width]']").val();
   console.log(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" step="any" 
name="properties[wallpaper_size_width]" class="spb-productdescfont 
spb-productoptiontextcolor spb-productoptionbackground" 
placeholder="Breite in cm" required="">

<button class='getVal'>Click me</button>

Ad
source: stackoverflow.com
Ad