Found 10711 Articles for Web Development

How to use jQuery to get the value of HTML attribute of elements?

Amit D
Updated on 18-Dec-2019 08:20:27

200 Views

To get the value of html attribute, use the val() method.ExampleYou can try to run the following code to learn how to use jQuery to get the value of HTML attribute of elements:Live Demo                          $(document).ready(function() {             $("#buttonSet").click(function () {                           $("#buttonGet").removeAttr('disabled');             });             $("#buttonGet").click(function () {               $("#result").html(               $("#txtBox").val() + "" +               $("input:radio[name=rd]:checked").val()             );          });          });                     Name                   Gender       Male       Female                            

How can I add an attribute into specific HTML tags in jQuery?

Amit D
Updated on 18-Dec-2019 08:19:15

904 Views

Use the attr() method to add an attribute into specific HTML tags in jQuery.ExampleYou can try to run the following code to learn how to add an attribute into specific HTML tags:Live Demo   $(document).ready(function(){       $(document).ready(function(){          $("button.button1").attr("myid", "myvalue");            $('button').on('click', function() {                     if (this.hasAttribute("myid")) {                alert('True: The new attribute added successfully.')             } else {                alert('False')             }           })       });   });           Button 1    

How do I find a certain attribute exists or not for a selected item in jQuery?

Amit D
Updated on 18-Dec-2019 08:41:53

94 Views

To find a certain attribute exists or not for a selected item in jQuery, use the hasAttribute() method.ExampleYou can try to run the following code to learn how to find a certain attribute exists or not:Live Demo                          $(document).ready(function(){             $('button').on('click', function() {                 if (this.hasAttribute("myattr")) {                    alert('True - myattr attribute exists')                 } else {                    alert('False - myattr attribute does not exist')                 }             })          });                           Button One                   Button Two          

How to use *= operator in jQuery attribute selector?

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

385 Views

The *= operator is used to filter elements for attribute containing the given value.ExampleYou can try to run the following code to learn how to *= operator to filter attributes on the basis of text:Live Demo $(document).ready(function(){    $( "div[myattr*='subject']" ).css("background-color", "yellow");     }); Java HTML Ruby

How to find an element based on a data-attribute value in jQuery?

Amit D
Updated on 18-Dec-2019 08:39:29

430 Views

To find an element based on a data-attribute value using jQuery is quite easy.ExampleYou can try to run the following code to learn how to find an element based on a data-attribute value using jQuery:Live Demo    $(document).ready(function() {       $('[data-slide="2"]').addClass('demo');    }); .demo {     font-size: 200%;     color: green; } One Two Three

How to use OR Operation in jQuery Attribute Selectors?

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

136 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

2K+ 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

866 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

319 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

Advertisements