Found 766 Articles for JQuery

How to use OR Operation in jQuery Attribute Selectors?

Amit D
Updated on 18-Dec-2019 08:38:40

114 Views

Use comma to work with OR operation in jQuery Attribute Selector.ExampleYou can try to run the following code to learn how to use OR operation in jQuery attribute selector:Live Demo                          $(document).ready(function(){            $('[myattr=demo][myid="sub1"],[myattr=demo][myid="sub3"]').css("background-color", "yellow");          });                     Java       HTML       Ruby    

How to use jQuery hasAttribute() method to see if there is an attribute on an element?

Amit D
Updated on 18-Dec-2019 10:07:36

1K+ Views

Use jQuery hasAttribute() method to see if there is an attribute for an element.ExampleYou can try to run the following code to learn how to use hasAttribute() method:Live Demo                          $(document).ready(function(){            $('button').on('click', function() {              if (this.hasAttribute("style")) {                alert('True')              } else {                alert('False')              }                         })          });                      Check for attribute in the button element             Button                   Button 2          

How to make jQuery attribute selector case insensitive?

Amit D
Updated on 06-Dec-2019 11:02:22

770 Views

To make jQuery attribute selector case insensitive, create two buttons and manipulate the input with attribute selector.ExampleYou can try to run the following code to make jQuery attribute selector case insensitive:Live Demo                          $(document).ready(function(){            $("button").click(function(){               $( "input[name*='myclass' i]" ).css("background-color", "yellow");            });                $("#insensitive").click(function(){               $( "input[name*='myclass' i]" ).css("background-color", "blue");            });            // myclass input blue            $("#sensitive").click(function(){               $( "input[name*='myclass']" ).css("background-color", "blue");            });          });                                       Case insensitive       Case sensitive    

How to pass a variable into a jQuery attribute-contains selector?

Amit D
Updated on 06-Dec-2019 11:09:32

3K+ Views

Yes, it is possible to pass a variable into a jQuery attribute-contains selector. The [attribute*=value] selector is used to select each element with a specific attribute and a value containing a string.ExampleYou can try to run the following code to learn how to pass a variable into a jQuery attribute-contains selector:Live Demo $(function() {   var testString = "vp-485858383";   $('[data-vp*="id: ' +  testString + '"]').css('color', 'blue'); }); Hello World

What is the difference between switchClass() and toggleClass() methods in jQuery?

Amit D
Updated on 14-Feb-2020 05:30:24

297 Views

The switchClass() is used to switch class from an element. Use it, to replace one class with another. Add jQuery UI library to the web page to use switchClass().ExampleYou can try to run the following code to learn how to work with switch class −Live Demo                          $(document).ready(function(){           $("a").click(function(){              $("a.active").removeClass("active");              $(this).addClass("active");            });          });             ... Read More

How to use removeClass() method in jQuery?

Amit D
Updated on 14-Feb-2020 05:28:00

70 Views

To add a class on click of anchor tag using jQuery, use the addClass() method. To remove a class, use the removeClass() method.ExampleYou can try to run the following code to learn how to remove a class using jQuery −Live Demo                          $(document).ready(function(){           $("a").click(function(){              $("a.active").removeClass("active");              $(this).addClass("active");            });          });                      .active {             font-size: 22px;            }                     One       Two       Click any of the link above and you can see the changes.    

How to use hasClass() method in jQuery?

David Meador
Updated on 14-Feb-2020 05:26:57

80 Views

The hasClass() method is used to check if an element has a class. You can try to run the following code to learn how to use hasClass() method in jQuery −ExampleLive Demo                          $(document).ready(function(){              $("button").click(function(){                  alert($("h1").hasClass("myclass"));              });          });                      .myclass {            color: blue;          }                     Heading       This is demo text.       Check: The h1 element has 'myclass' class?    

How to get and set form element values with jQuery?

David Meador
Updated on 14-Feb-2020 05:24:52

11K+ Views

To get a form value with jQuery, you need to use the val() function. To set a form value with jQuery, you need to use the val() function, but to pass it a new value.ExampleYou can try to run the following code to learn how to get and set form element values with jQuery −Live Demo                          $(document).ready(function(){                        $("#buttonSet").click(function () {                  $("#txtBox").val("Amit");                  $("input:radio").val(["male"]);                  $("#buttonGet").removeAttr('disabled');             });              $("#buttonGet").click(function () {                 $("#result").html(                     $("#txtBox").val() + "" +                     $("input:radio[name=rd]:checked").val()                 );             });          });                     Name                   Gender       Male       Female                            

How to get the closest input value from clicked element with jQuery?

David Meador
Updated on 14-Feb-2020 05:23:03

7K+ Views

To get the closest input value from clicked element with jQuery, follow the steps −You need to use closest to find the closest ancestor matching the given.Now, find the input with the given name using the attribute equals selector.The last step is to use val() metjod to retrieve the value of the input.Let’s first see how to add jQuery −ExampleYou can try to run the following code to get closest input value from clicked element with jQuery −Live Demo                          $(document).ready(function(){           ... Read More

How to get the input value of the first matched element using jQuery?

David Meador
Updated on 14-Feb-2020 05:21:14

791 Views

To get the input value of the first matched element using jQuery, use the .first() method.ExampleYou can try to run the following code to get the input value of the first matched element using jQuery −Live Demo                          $(document).ready(function(){            $( "li" ).first().css( "background-color", "blue" );          });                              One          Two          Three          Four          

Advertisements