javascript - for each element of a class, check the contents to see if it contains a string, and then update only that element if it does -


code version 1:

        var 1 = "one";         $.each($(".buttons"),(function() {             if ($("this:contains(one)")) {                 $(this).addclass( "currentbutton" );              };         })); 

code version 2:

        var 1 = "one";         $(".buttons").each(function(a, e) {             if ($("e:contains(one)"))             { $(e).addclass( "currentbutton" ); };         }); 

i think see i'm trying do. problem updating specific element text matched, elements updated when 1 matches.

edit: html below:

<input type="submit" class="buttons" value="one"> <input type="submit" class="buttons" value="two"> <input type="submit" class="buttons" value="one & two"> 

i using inputs programmatically added buttons using asp.net/c#

i have attempted couple of solutions , i'm still having every element have class added.

i updated this jsfiddle inputs , it's not being affected @ all. i'm guessing :contains won't check input value.

using original method fixed follows, or alternatively see answer elias:

        $(".buttons").each(function() {             if ($(this).attr("value") == one) {                 $(this).addclass("currentbutton");             };         }); 

the easiest way you're doing one-liner, though:

var 1 = "one"; $(".buttons").filter(":contains('"+one+"')").addclass("currentbutton"); //or $(".buttons").filter(":contains('one')").addclass("currentbutton"); 

check fiddle

this imply :contains selector constant value, if need change according somethign else, wrap in function:

function changeclass(contains) {     contains = contains || 'one';//default value     $('.buttons').filter(":contains('"+contains+"')").addclass('currentbutton'); } 

of course, always, can change parameters function more reusable:

function changeclass(selector, filter, newclass) {     $(selector).filter(filter).addclass(newclass); } changeclass('.buttons', ':contains("one")', 'currentbutton'); 

your problems being caused enclosing either this or e inside string delimiters, turned them string constants, not references dom nodes trying change
happened both:

if ($("e:contains(one)")) {     $(e).addclass( "currentbutton" ); } 

and

if ($("this:contains(one)")) {     $(this).addclass( "currentbutton" ); } 

evaluated :

if ([]) {     $(this).addclass('currentbutton'); } 

in other words: passing string constants main jquery function ($()) tried make bet of things, , treated them selectors. sadly, came empty, empty array-like jquery object returned, , object/array truthy value in js, expressions checked evalueted true, hence, nodes classes changed.
have written:

if ($('foobar, see if matters write here')) {     console.log('it doesn\'t'); } 

and it'll log it doesn't time , time again.

edit

in response comment, if want filter, based on elements' value attribute:

$('.buttons').filter(function() {//this function applied each element returned $('.buttons') selector  //only callback returns true currentbutton class     return /\bone\b/i.test($(this).val());     //or, equally valid:     return ($(this).val().indexof('one') !== -1); }).addclass('currentbutton'); 

note /\bone\b/i accept "one" "one" or "one", won't return true if value attribute contains "bones", whereas indexof casesensitive, doesn't check if one part of word or not.
case sensitive regex, can use /\bone\b/, without i.

a more strict, lot shorter version of same thing be:

$('.buttons[value~="'+one+'"]').addclass("currentbutton"); 

but there many jq selectors can use, best keep reference close chest


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -