Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery value selector

I have:

<button class="Delete" value="1">Delete</button>
<button class="Delete" value="2">Delete</button>
<button class="Delete" value="3">Delete</button>

Given variable X that contains a value (in this case either a 1, a 2 or a 3), then how do I hide the button corresponding to the value in X?

I want to say something like:

$('button').val(x).hide();

Meaning: "The button whose value is x, hide".

like image 334
Phillip Senn Avatar asked Feb 11 '11 21:02

Phillip Senn


People also ask

How can we set values in jQuery?

Set Content - text(), html(), and val() We will use the same three methods from the previous page to set content: text() - Sets or returns the text content of selected elements. html() - Sets or returns the content of selected elements (including HTML markup) val() - Sets or returns the value of form fields.

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.

What is the jQuery selector?

jQuery selectors allow you to select and manipulate HTML element(s). jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more.

How do I select a specific Dropdownlist using jQuery?

Syntax of jQuery Select Option $(“selector option: selected”); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $(“selector option: selected”).


4 Answers

$('button[value="' + x + '"]').hide();
like image 95
Šime Vidas Avatar answered Oct 05 '22 23:10

Šime Vidas


You can write your own custom selector (I just felt someone should mention it). Could look like:

(function($) {
    $.extend($.expr[':'], {
         val: function(elem, i, attr) {
             return elem.value === attr[3];
         }
    });
}(jQuery));

$('button:val(2)').hide();
like image 45
jAndy Avatar answered Oct 05 '22 23:10

jAndy


You'd do that inside of the actual selector:

$('button[value="foo"]').hide();
like image 38
Blender Avatar answered Oct 05 '22 23:10

Blender


This would require concatenating x into an xpath, but i can't think of another way right now.

$(button[value='1']).hide();

EDIT: removed @, apparently deprecated, much like myself :)

like image 39
hollystyles Avatar answered Oct 05 '22 23:10

hollystyles