Javascript update value based on click event

onclick javascript
javascript change textbox value onclick
javascript set hidden field value before submit
div onclick javascript
change value of input field on click
jquery set value
get textbox value in jquery on button click
onclick get value in jquery

I have three buttons on the page and I want to update the value of a variable based on each click event. I have done something like below but I think that is not the right approach I need something generic which follows the best practice but I don't know how. your help will be highly appreciated.

 $('#digital_story_cover_image #imageset-fancybox').click(function() {
     list = $('.cover-image');
 });

 $('#gallery_main_images #imageset-fancybox').click(function() {
     list = $('.gallery-images');
 });

 $('#gallery_main_images #multi-imageset-fancybox').click(function() {
     list = $('.gallery-images');
 });

Thanks.

We can try avoiding multiple lines of code.

Just write event click event handler for all the selectors. like below

$('#digital_story_cover_image #imageset-fancybox, #gallery_main_images #imageset-fancybox, #gallery_main_images #multi-imageset-fancybox').click(function(e) {
     list = $($(e).parent().attr('data-list-selector'));
 });

HTMLElement: change event, When the user commits the change explicitly (e.g., by selecting a value from a <​select> 's dropdown with a mouse click, by selecting a date from a  Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.

Like this to make recalling those accessors easier.

const digId = $('#digital_story_cover_image #imageset-fancybox');

digId.click(function() {
     list = $('.cover-image');
 });

const galId = = $('#gallery_main_images #imageset-fancybox');

galId.click(function() {
     list = $('.gallery-images');
 });

const galIdMulti = $('#gallery_main_images #multi-imageset-fancybox');

galIdMulti.click(function() {
     list = $('.gallery-images');
 });

Same could be done for all accessors seen here. e.g.

const galImg = $(‘.gallery-images’);

HTML onclick Event Attribute, Click on a <p> element to change its text color to red: <p id="demo" onclick="​myFunction()">Click me to change my text color.</p> <script> function myFunction()  For our example, the function called onclick updates the total field in the form using the value of the checkbox clicked. The complete JavaScript for the form is displayed below. JavaScript for Checkboxes Onclick Demo. The JavaScript to handle clicks on the checkboxes and update the total is displayed here with explanation below:

This looks fine to me too, as @Lloyd Nicholson said: It defines certain outcomes and they are all specific to what is needed in your project.

However, you could refactor this and end up with only two on click events like so:

 $('#digital_story_cover_image #imageset-fancybox').click(function() {
     list = $('.cover-image');
 });

 $('#gallery_main_images #imageset-fancybox, #gallery_main_images #multi-imageset-fancybox').click(function() {
     list = $('.gallery-images');
 });

As you actually do the same thing in the last two events you have, you can use the same 'on click event' on multiple selectors using a comma between them.

*I think it would be good to have your code and more explanation on your issue, so we can provide better answers. I hope this is what you needed though.

HTML DOM Input Hidden value Property, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  For instance, I know that it is possible to do something in Javascript that allows users to update text based on user text input: <script type="text/javascript">; function changeText2(){ var

jQuery Set Content and Attributes, The jQuery attr() method is also used to set/change attribute values. The following example demonstrates how to change (set) the value of the href attribute in a link  JavaScript DOM: Exercise-6 with Solution. Write a JavaScript function that accept row, column, (to identify a particular cell) and a string to update the content of that cell.

.val(), Get the current value of the first element in the set of matched elements or set the value of every matched element. <script src="https://code.jquery.com/jquery-3.5​.0.js"></script> the elements of the array will be unchecked or unselected, depending on the type. <p>Type something and then click or tab out of the input. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! But avoid … Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience. To learn more, see our tips on writing great

.change(), Description: Bind an event handler to the "change" JavaScript event, or trigger that event The change event is sent to an element when its value changes. After this code executes, clicks on Trigger the handler will also alert the message. I am trying to write a basic function that will decide whether the object already exists in array of objects based on unique id that is inside this object as a property. Upon searching I came up with this code, and it seems to be doing the job fine, but I can imagine there is a better and faster way to determine whether to push or update the

Comments
  • Depends on what you want to do, post your html and explain.
  • what is that variable list do?
  • What you have done is acceptable if it works. It defines certain outcomes and they are all specific to what is needed in your project
  • variable list appends the uploaded image to the list.
  • Why don’t you create variables for each of those gallery ids and then reuse won’t be difficult or time consuming - var digId = $(‘#digital_story_cover_image #imageset-fancybox’); do that for each of your long accessors