Milena Jażdżyk Milena Jażdżyk - 6 months ago 7
jQuery Question

JQuery get the 9th character in a number

I have to get the 9th character in a barcode (10361510700149) and when this char is equal to 7 it will set my checkbox as "checked".

Don't laugh, I wrote sth but of course it doesn't work. It's my first journey with JQuery :D

[event: , load,
var bar = $(”#barcode”).val();
var bars = bar.toString();
var nine = bars.charAt(8);
var nine_as_number = parseInt(nine);
if (nine_as_number == 7) {
$(’.c1540’).prop(’checked’, true);
}
]

Answer

Use this code

$("span").each(function(){
    if ($(this).text().charAt(8) == 7)
        $(this).siblings("input").attr("checked", true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
    <span>10361510600149</span>
    <input type="checkbox" />
</div>
<div>
    <span>10361510700149</span>
    <input type="checkbox" />
</div>
<div>
    <span>10361510800149</span>
    <input type="checkbox" />
</div>

Comments