How to activate an element using jQuery and JavaScript. JQuery - set attribute value


Sometimes you need to make a text field, radio buttons or checkbox active or inactive. How to do this dynamically without reloading the page? You can use JavaScript and jQuery prop for this. An HTML element is enabled by setting the disabled property to true .

With jQuery, you can select an element to turn on or off and change that property using the prop() or attr() functions, depending on which version of jQuery you're using.

The prop() function was added in jQuery 1.6 and is the standard method for working with properties. And the attr() function does the same thing in jQuery 1.5 and below.

It is also possible to enable or disable any HTML element using JavaScript. All you need to do is find the element by id and set its disabled property to true or false .

How to make a text field active/inactive using JavaScript

This example has an HTML form, text fields, and a couple of buttons to make the text field active or inactive. Here I use simple javascript, without jQuery prop checked .

The steps will be as follows:

  • Use the enable() and disable() functions with buttons to turn a text field on or off.
  • Use getElementById() to access the text field.
  • Set the disabled field to true or false .
  • Here is an example HTML file with a JavaScript-based solution:

    How to turn input on or off using JavaScript

    Enabling and disabling input using JavaScript
    Enter your name:
    Disable text field Activate text field function disable() ( document.getElementById("name").disabled = true; ) function enable() ( document.getElementById("name").disabled = false; )

    When you click on the “Deactivate text field” button, the disable() function is called (not to be confused with jQuery prop disabled), and the disabled property of the text field is set to true. Therefore, you can no longer enter text in this field; it is not active. In this case, you can reactivate the text field by clicking on the “Activate text field” button. It will call the enable() function, which will reset the disabled property to false .

    How to enable/disable textbox using jQuery?

    Below is jQuery based code that does the same thing. In this example we used the prop() function. If you are using jQuery 1.5 or older, replace prop() with attr() .

    Just like in the previous example, we have two buttons btn_enable and btn_disable to turn the text field on and off. Let's connect the event handler using the click() function, which is called when the page is loaded.

    In the event handler, we find the text field using jQuery's ID selector, for example $("#name") and then call prop("disabled", false") to disable that text field.
    When the user clicks the "enable" button, we set the disabled property to true by calling jQuery prop("disabled" , true ). This will re-activate the text field. Remember that you cannot enter text into a disabled field.

    How to turn a text field on or off using jQuery Turning a text field on or off using jQuery Enter your name:
    Disable input Enable input

    $(document).ready(function() (
    $("#btn_enable").click(function())( $("#name").prop("disabled", false); ));
    $("#btn_disable").click(function())( $("#name").prop("disabled", true); ));
    });

    You can test this by running the example in your browser. Just don't call removeProp() to re-enable the button. This will remove the "disabled" attribute from the text field, so it won't be able to be disabled again in the future. Use jQuery's prop disabled method instead.

    Here's a screenshot of what the page would look like in a browser like Edge or Chrome.

    In this example, we disabled the text field and re-enabled it. You can use JavaScript or jQuery for this.

    Translation of the article “How to enable/disable an element using jQuery and JavaScript? Example” was prepared by the friendly project team.

    Good Bad

    I have the following HTML code with two elements having the same name

    Through JQuery I want to set the enable checkbox. So I use something like this:

    $("#chk0").attr("disabled",false);

    But it doesn't work. I'm guessing JQuery is getting confused with two elements with the same name. Unfortunately, I can't avoid using two different names because when the form is posted, I want all the checkboxes to be placed (not just the ones that are checked). Hence I have to use a hidden element with the same name. So back to the question, how can I enable the checkbox via JQuery in the above script? Is there a "type" parameter to attr that is separate from the checkbox?

    6 answers

    Some things down to the actual code.

    the hash(#) you use as a selector is for identifiers, not element names. also a disabled attribute is not a true false scenario.. if it disabled the attribute it means it is true.. you need to remove the attribute and not set it to false. There are also form selectors that identify specific types of elements in a form.

    so the code will be

    $("input:checkbox").removeAttr("disabled");

    Bringing the answer up to date

    You should use the .prop() method (added after v1.6)

    $("input:checkbox").prop("disabled", false); // to enable the checkbox

    $("input:checkbox").prop("disabled", true); // to disable the checkbox

    Seriously, just don't use jQuery for this. disabled is a boolean property of form elements that has worked fine in every major browser since 1997, and there is no way that it is easier or more intuitive to change whether a form element is disabled.

    AND JavaScript string to enable this checkbox:

    Document.getElementById("chk0_checkbox").disabled = false;

    If you prefer, you can use jQuery to set the checkbox instead:

    $("#chk0_checkbox").disabled = false;

    "True" and "False" do not work, disable, set to "disabled".

    $(".someElement").attr("disabled", "disabled");

    To enable, remove.

    $(".someElement").removeAttr("disabled");

    Also, don't worry about selecting multiple elements, jQuery will work on all of them that match. If you only need one, you can use many things: first, last, nth, etc.

    You are using the name, not the id, like the other mention - remember that if you are using id valid xhtml, the IDs need to be unique.

    Animation looks pleasing to the eye, but creating animation is not an easy job. It’s important to take into account the effect that animations can have on performance, as even the coolest animations can have negative effects and downgrade user experience. Luckily, jQuery has some inbuilt methods to implement simple animations with ease. These methods are named in such a way that they are clearly labeled for their use and functionality. In this short post, we’ll look at each of these methods in action. These animation methods have one thing in common - accepted parameters. The following 3 parameters are applicable for all methods:
    • Duration: Optional parameter. Either string or number specifications can be used to determine the amount of time given for the animation to complete. The default value is 400 milliseconds. It also accepts “slow” or “fast” string values ​​where “fast” indicates duration of 200 milliseconds and “slow” indicates 600 milliseconds.
    • Easing: Optional. Easing indicates the speed of the animation at different points during the animation. jQuery provides inbuilt swing and linear easing functions to help you play with this parameter.
    • Callback: Optional. A function that may be executed after the animation is complete.
    fadeIn() The fadeIn() method gradually changes the opacity of the selected element from hidden to visible. It changes the opacity value from 0 to 1. It will not work on hidden items. To use this method,

    $("#elm").fadeIn("fast");
    $("#elm").fadeIn(1000); // 1 second

    fadeOut() As you may have guessed, this is the opposite of fadeIn(). This method gradually changes the opacity of the selected element from visible to hidden. It changes the opacity value from 1 to 0. Once the opacity reaches 0, the jQuery set display property is set to none. To use this method:

    $("#elm").fadeOut("slow");
    $("#elm").fadeIn(2000); // 2 seconds

    fadeTo() The fadeTo() method is similar to the fadeIn() method but it gives the option to control the opacity of the element. You can specify the opacity value, as shown here:

    $("#elm").fadeTo("slow", 0.3); //The opacity value is 0.3

    The above code will change the opacity of the element gradually to 0.3.

    fadeToggle() This method is a combination of fadeIn() and fadeout(). If the elements are faded out, fadeToggle() will fade them in and when the elements are faded in, fadeToggle() will fade them out. Like this:

    $("#elm").fadeToogle("slow");
    $("#elm").fadeToggle("slow", function())(
    console.log("The fadeToggle is finished!");
    });

    slideUp() The slideUp() method slides-up the selected element to hide it. This method internally controls the height of the element. Once the height reaches 0, it sets the display property to none in order to hide it. To implement it, use the following code:

    $("#elm").slideUp(); //Uses default value of 400 milliseconds for duration.
    $("#elm").slideUp(500);

    slideDown() This method works opposite to the slideUp() function. It slides-down the selected element to show it. This method changes the height of the matched elements from 0 to the specified maximum height. Thus, it gives the element a slide down effect. To implement it, use the following code:

    $("#elm").slideDown(); //Uses default value of 400 milliseconds for duration.
    $("#elm").slideDown(1000);

    slideToggle() This method is a combination of slideUp() and slideDown(). Internally this method also makes use of the display style property. While sliding up, when the height of the element reaches 0 it sets the display style property to "none". While slide down it sets the "inline" value of the display property. Based on the value of the display property, it determines whether to slide up or down. To implement it:

    $("#elm").slideToggle("slow", function() (
    console.log("Animation complete.");
    });

    Bonus tip: If you want to disable all the animation, set jQuery.fx.off property to ‘true’. This is quite useful when your jQuery code uses animation heavily and you need to disable it. Instead of visiting every single line and modifying it, it’s better to use this property to disable animation completely.

    Conclusion This post talks briefly about various inbuilt animation methods available in jQuery. These methods can perform animations like fading and sliding in both directions. These methods also accept duration to control the speed of your animation. They are simple to use and allow you to implement simple animation with hardly any effort. Have fun playing with them!

    Steps is a simple, lightweight and easy to configure jQuery plugin which allows you to create a step wizard from any grouped elements. This plugin is simple to use and it is only dependent on jQuery library. It comes with various options for customizing the wizard. You can:

    • Set the starting number which starts the wizard at a specific step number
    • Implement ‘showBackButton,’ which indicates whether the back button will be visible or not – quite useful if you want to restrict your users from going back to the previous step
    • Implement ‘show/hide’ footer buttons – when footer buttons are not visible, then the user can move back and forth on clicking the step number buttons
    2.formToWizard

    FormToWizard is a jQuery plugin which allows you to turn any web form into a multi-step wizard with the help of jQuery library. In order to determine the number of steps involved, this plugin first selects all fieldsets and opts for the size of this wrapped set. Next, it iterates through this wrapped set (that returned all fieldsets), wraps each fieldset into a div and appends a paragraph that will hold the “back” and “next” buttons. You can also customize the name of the previous and next buttons.

    3. SmartWizard http://techlaboratory.net/smartwizard

    SmartWizard is a flexible and heavily customizable jQuery step wizard plugin with Bootstrap support. It is easy to implement and gives a neat and stylish interface for your forms, checkout screen, registration steps etc. There are a lot of features on top of the built-in Bootstrap support, including:

    • Responsive themes
    • Customizable toolbars
    • URL navigation and step selection
    • Customizable options
    • Public methods,
    • Event support,
    • Keyboard navigation
    • Multiple wizard instances on the same page
    A neat thing about SmartWizard is that the author of this plugin regularly updates the plugin based on the user’s feedback. Based on the user’s feedback, the recent version 4.0 has been completely rewritten from scratch, making it more powerful, robust, scalable, and customizable.4. Material Bootstrap Wizard https://github.com/creativetimofficial/material-bootstrap-wizard

    The Material Bootstrap Wizard is a jQuery plugin which is a fully responsive wizard, inspired by the famous Google's Material Design. It is one of the handiest elements that can be used inside a project. Material Bootstrap Wizard breaks the long html form into chunks and lets the user see it one step at a time. This way, the user only has to focus on the current step without being overwhelmed. They will, however, be able to see how many steps they have remaining, so they can assess approximately how long the process will be.

    5. Wizard.js https://github.com/jeffreypolk/bootstrap-wizard

    Wizard.js is a jQuery step wizard plugin which uses the bootstrap"s modal component to display any type of content in a step-by-step wizard. It takes care of the plumbing for moving back and forth between steps, validation, hooks for key events, etc. The wizard depends on structured HTML to define the wizard and the steps for it. The order of the steps presented is defined by the order of the divs within the wizard. There are lots of customization options available to play around. Each step of a wizard can be validated before moving onto the next step using form validation or custom validation.

    6.Stepform https://github.com/masade/stepform
    StepForm is a simple and lightweight jQuery plugin that converts any form into a sliding form of step by step wizard. It is an ideal choice for creating sign-up forms. It also performs the validation using a data-validate attribute and so the user can slide to the next step only after any errors on inputs on the current slide have been validated. It also supports keyboard interactions using enter and tab keys to move to the next step or to the next field.7. jQuery Steps https://github.com/rstaib/jquery-steps

    JQuery Steps is a smart UI component which allows you to easily create wizard-like interfaces. This plugin groups content into sections for a more structured and orderly page view. Furthermore, it is as simple as 1-2-3 to add plugins such as jQuery Validation which can prevent step changing or submission. It supports:

  • HTML5 support
  • Accessibility support
  • State persistence
  • Form validation using any validation plugin of your choice
  • Cool transition effects
  • Easy Navigation
  • Keyboard navigation
  • Multiple wizards on the same page
  • It is very lightweight (only 2.9KB minified) and works with all modern browsers.

    Returns or modifies the property value of selected page elements (the difference and relationship between attributes and properties). The function has four use cases:

    returns the attribute value propName at the selected element. If several elements are selected, the value will be taken from the first one. The method returns undefined if the first of the selected elements does not have the specified property set or the set of selected elements is empty.

    in all selected elements, property propName will take the value value.

    in all selected elements will change the values ​​of the property group propName1, propName2, ... making them equal value1, value2, ... respectively.

    property propName the value returned by the user-defined function will be assigned (if it does not return anything, then the property will simply not change its value). The function is called separately for each of the selected elements. When called, she is given following parameters: index— the position of the element in the set, value— current value of the property propName at the element.

    Examples of use:

    Note 1: It is important to note that using the .prop(name) method, you will only get the property values ​​for the first element of all the selected ones. If you need the values ​​of all elements, then you should use constructs like .map() or .each() .

    Note 2: in IE up to version 9, use prop to set values ​​other than simple types(number, string, boolean) causes a memory leak if this property is not removed (using .removeProp()) before removing this element from the DOM. Therefore, to set your own data to DOM objects, it is better to use .data() .

    Note 3: Don't use .removeProp() as the equivalent of setting a property to false . If you delete a native (native, created by the program, not the user) property of a DOM object, you will not be able to return it later.

    Prop() is convenient for working with attributes that do not require specifying values ​​(checked and disabled in input elements), in most other cases, it is better to use .attr() to work with attributes.

    In action

    Let's make all checkboxes inactive:

    ~lt~!DOCTYPE html~gt~ ~lt~html~gt~ ~lt~head~gt~ ~lt~style~gt~ img(padding:10px;) div(color:red; font-size:24px;) ~lt~/style~gt~ ~lt~script src="http://code.jquery.com/jquery-latest.js"~gt~~lt~/script~gt~ ~lt~/head~gt~ ~lt~body~gt~ ~lt~input type="checkbox" checked /~gt~ ~lt~input type="checkbox" /~gt~ ~lt~input type="checkbox" /~gt~ ~lt~ input type="checkbox" checked /~gt~ ~lt~script~gt~ $("input").prop(( disabled: true )); ~lt~/script~gt~ ~lt~/body~gt~ ~lt~/html~gt~

    Links
    • List of all functions for manipulating page elements

    Search Keys:

    • properties of elements on the page
    • property value
    • get property value
    • change property value
    • set property value
    • assign a value to a property
    • .prop()
    • prop()




    

    2024 gtavrl.ru.