I'm trying to assign the 'for'
attribute to each label tag instead of manually typing it. In addition, I also want to make each 'for'
equal it's corresponding input 'name'
How can I use Jquery to write code once that dynamically changes each one?
Below is my html.
I tried doing this:
$("input, select").each(function(){
$(this).siblings().attr('for', [input=name].val)
});
but that doesn't work. Any Ideas?
$("input, select").each(function(){
$(this).siblings().attr('for', [input=name].val)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="billingArea">
<p>
<label>First Name:</label>
<input name="billingFirstName" type="text" value="" required />
</p>
<p>
<label>Last Name:</label>
<input name="billingLastName" type="text" value="" required />
</p>
<p>
<label>Street Address:</label>
<input name="billingAddress1" type="text" value="" required />
</p>
<p>
<label> Street Address 2: </label>
<input name="billingAddress2" type="text" value="" />
</p>
<p>
<label> City: </label>
<input name="billingCity" type="text" value="" required />
</p>
<p>
State/Province: <select name="billingState" required />
<option value="">--</option>
<option value="Alberta, Canada">AB</option>
<option value="Alaska, USA">AK</option>
</select>
</p>
<p>
<label>Postal Code:</label> <input name="billingZip" type="text" value="" required />
</p>
</div>