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".
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.
$() = 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.
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.
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”).
$('button[value="' + x + '"]').hide();
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();
You'd do that inside of the actual selector:
$('button[value="foo"]').hide();
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With