angular implementation of toggle and click function

Is it possible to dynamically change the ng-click function of a button based on user interaction?

For example, can we toggle between two functions using something similar to toggling classes: ng-class: "functionToggle? 'disabled': 'enabled'"

In this case, when the value is a function like in the following example: ng-click="funnyFunction()"

I am looking to call funnyFunction() if the user selects certain options in a form and seriousFunction() if different ones are selected. Ideally, I would like to achieve this by using the same button but changing the function that is called based on user interaction.

Answer №1

Absolutely, you can achieve the desired outcome by following a different approach. Avoid putting your logic directly in HTML; instead, use JavaScript for this purpose. Here's an example:

<button onclick="myFunction()">Click here</button>

Then, in your JavaScript code:

function myFunction() {
  if(condition) {
    doSomething();
  } else {
    doSomethingElse();
  }
}

Answer №2

To implement the functionality, utilize ng-click="this[method]()"

  <form action="">
    <input ng-click="executeFunction(choice.select)" ng-model="choice.select" type="radio" name="gender" value="function1"> Function1<br>
    <input ng-click="executeFunction(choice.select)" ng-model="choice.select" type="radio" name="gender" value="function2"> Function2<br> 
  </form>

In the form above, the user selects an option which determines the 'Button' label to change to a specified function.

  <button ng-click="this[choice.select]()">
    Button 
  </button>

See it in action on Jsfiddle: http://jsfiddle.net/mkarrfan/o89yguk9/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The first parameter in Vue router is optional

Is there a way to make the first parameter in my list of routes optional? I want the parameter to change a header in my api calls if it's present in the url, but everything else should stay the same. Instead of creating two sets of routes to handle t ...

Using JavaScript regex to eliminate content enclosed in parentheses, brackets, and Cyrillic characters

Is there a way to transform (Test 1 / Test 2) [Test 3] Отдел / Here is title - subtitle (by Author) - 1234 (5678-9999), descriptions (best), more descriptions into Here is title - subtitle (1234) (descriptions) using a combination of JavaScript an ...

Launch the jQuery Fancybox on a separate webpage

I am facing an issue where I have a link in my index.html page and I need it to open a div located in another page named index2.html, but for some reason, it is not working. This is the code I currently have: Link in index.html <a href="#modal" id="o ...

Adjusting attention to the switched element

I am currently developing an inline editing feature using AngularJS. Here is the code snippet from my Controller: $scope.toggleEditor = function(){ $scope.editorEnabled = !$scope.editorEnabled; //toggle editor view //struggling with this part $time ...

Using jQuery to toggle visibility on click within WordPress

Looking for a way to make three buttons change color on hover and display different content when clicked? You're not alone! Despite searching through tutorials and forums, the solution remains elusive. The buttons are structured like this: <div i ...

What is the best way to instruct Vite to exclude specific files within a directory from the build process?

After initializing a fresh Vue application with the command npm create vue, I implemented a functionality where the app fetches a configuration at runtime and extracts a component name from it. These dynamic components reside in a directory called "pluggab ...

What is the best way to determine the count of elements in an array that have the active property set to true?

Can anyone help me figure out the most efficient way to solve this problem? (Filter, ng-repeat, or another method?) <div>Number of active items: {{product.length}} </div> //total number of items <div>Number of inactive items: {{product.l ...

What is the reason for Cloud Firestore's realtime database fetching all items?

Currently, I am developing a project with vuejs and I am facing an issue where I need to fetch only the last created item from Firestore's realtime database. However, when I execute the following code snippet, it retrieves all items from the database ...

Modifying Data with MomentJS when Saving to Different Variable

After attempting to assign a moment to a new variable, I noticed that the value changes on its own without any modification from my end. Despite various attempts such as forcing the use of UTC and adjusting timezones, the value continues to change unexpec ...

What is the best way to place a 3D model at random points on the surface of a sphere while ensuring that it always faces the right direction?

I'm faced with the challenge of placing huts randomly on a spherical world. While this task is feasible, the issue arises when the huts do not sit correctly - their bottom should be in contact with the tile below. I've experimented with the &apos ...

Retrieving the output from a nested scope within a function

I have a scenario where I am working with a function that has a nested "then" function containing a return statement. My goal is to combine this inner return data with the outer return data. Below is the code snippet: public async getAllWidgets(): Promis ...

Encountering difficulties in storing array data into MongoDB collection

I am facing an issue with connecting to two different MongoDB instances using different URLs. One URL points to a remote connection string while the other one is for a local MongoDB instance. Initially, I establish a connection to MongoDB using MongoClient ...

Having difficulty showing the successful JSON output in either the view or an alert

In my CodeIgniter project, I have three input fields named name, emp_id, and crm_id. I enter the id value and send it to the controller via AJAX and JSON to retrieve all information related to that id. The issue is that while I can see the correct output i ...

Creating a unique WooCommerce product category dropdown shortcode for your website

I am having trouble implementing a WooCommerce categories dropdown shortcode. Although I can see the drop-down menu, selecting a category does not seem to trigger any action. Shortcode: [product_categories_dropdown orderby="title" count="0" hierarchical=" ...

Selecting options with a click of a button in Template-Driven Forms

Whenever the page loads, I always notice that the last radio button is automatically checked. I believe it has something to do with the Id attribute, but I'm struggling to find a solution. countryArray countryA = [{name : "Finland"}, {name : "china" ...

Error message HMR Webpack was found to be invalid

When using Webpack, I keep encountering the following error message: Invalid HMR message, along with a lengthy JSON string. Unfortunately, I couldn't find any helpful resources to assist me in debugging this issue. Do you have any suggestions? https ...

Invoking an express route using JavaScript

After successfully defining some routes, including a text input search field at the top of the page, I created a listener function as shown below: $('#tags').keypress(function(e) { if (e.keyCode == 13 && document.getElementById('t ...

Invoke the onClick function within a setInterval loop

My onClick function is functioning correctly. However, I want to run it periodically using setInterval after it's been triggered by a click event. Here's my attempt at modifying the code: var clickData; var isClicked=0; function onCl ...

Ways to position a button at the bottom of a Bootstrap card

In the card layout, I am struggling to position the red button at the bottom of the column despite using margin auto. Can anyone provide a solution for this issue? Thank you in advance! <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a ...

Centralize the storage of domain within an AngularJS service

In my Angular service, I have the following method: function send(data) { return $http({ method: 'POST', url: 'https://test.domain/test/send', data: $httpParamSerializerJQLike(data) ...