Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery select by attribute value not working - incase with space in value

In my checkboxes, i am using select the element using values.. for that (sample) i made like this..

<form>
    <input type="checkbox" value="one 234" />
</form>

function doit(){
    var x = "one 234"
    $(':checkbox[value='+x+']')
    .css({border:"1px solid green"})
}

doit();

But is doesn't work.. in case if i remove the space inbetween the value of "one 234" - it works. how to handle this kind of situation..?

here is the jsfiddle

like image 540
3gwebtrain Avatar asked Dec 16 '22 09:12

3gwebtrain


1 Answers

Use quotes :

$(':checkbox[value="'+x+'"]')

Or filter, which I personally find cleaner and with which you don't have to worry about any character escaping :

$(':checkbox').filter(function(){ return this.value===x })
like image 183
Denys Séguret Avatar answered Jun 12 '23 04:06

Denys Séguret