Possibility to find a clicked number in a value using jQuery

jquery select dropdown
jquery select text in div
jquery set select value
get textbox value in jquery on button click
jquery get input value by name
jquery get input value by id
jquery get selected option value
jquery set value of textbox

I want to learn, can we find the same number in two input values. For example:

<input type="hidden" id="multinumber" value="1,2,3,4,5">
<input type="hidden" id="justonenumber" value="3">

<div class="click">Click and test the same number</div>

JS

$(document).ready(function(){

  $("body").on("click",".click", function(){
    var multiple = $("#multinumber").val();
    var single = $("#justonenumber").val();
  });
});

When onClick event on the .click button then check the same number in the #multinumber and #justonenumber input values and get the result in an alert box.

Is there a way to do this ? Anyone can help me here please?

Just use indexOf or includes on your multiple string. :)

$(document).ready(function(){

  $("body").on("click",".click", function(){
    var multiple = $("#multinumber").val();
    var single = $("#justonenumber").val();
    var doesMultipleIncludeSingle = multiple.includes(single);
    // OR
    var doesMultipleIncludeSingle = multiple.indexOf(single) > -1;

  });
});

As per the problem explained in the comment, it seems the requirement does involve splitting the array.

$(document).ready(function(){

  $("body").on("click",".click", function(){
    var multiple = $("#multinumber").val().split(',');
    var single = $("#justonenumber").val();
    var doesMultipleIncludeSingle = multiple.includes(single);
    // OR
    var doesMultipleIncludeSingle = multiple.indexOf(single) > -1;

  });
});

Possibility to find a clicked number in a value using jQuery, Just use indexOf or includes on your multiple string. :) $(document).ready(​function(){ $("body").on("click",".click", function(){ var multiple  Users of older versions of jQuery should use .delegate() in preference to .live()." – miguelr May 6 '12 at 9:15. Yes, because using delegate on an element as close as possible to the elements you want the events from is better. But adding a delegate to document is actually the same as adding a live event.

You can get the value of first input box. Split it by , and check with .indexOf for the other input. If it's there, you can put the result in alert box like

$(".click").click(function(){

  var x = $("#multinumber").val().split(",");
  var y = $("#justonenumber").val();
  
  if(x.indexOf(y) > 0){
   alert(x.find(o=> o==y))
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="hidden" id="multinumber" value="1,2,3,4,5">
<input type="hidden" id="justonenumber" value="3">

<div class="click">Click and test the same number</div>

.val(), Get the value from the selected option in a dropdown A string of text, a number, or an array of strings corresponding to the value of each matched element to  The find () method returns descendant elements of the selected element. A descendant is a child, grandchild, great-grandchild, and so on. The DOM tree: This method traverse downwards along descendants of DOM elements, all the way down to the last descendant. To only traverse a single level down the DOM tree (to return direct children), use the

is this what you want?

$(document).ready(function(){

  $("body").on("click",".click", function(){
    var multiple = $("#multinumber").val();
    var single = $("#justonenumber").val();
    if(multiple.indexOf(single) > -1) alert(single + " is found");
    else alert(single + " isn't found");
  });
});

.select(), Description: Bind an event handler to the "select" JavaScript event, or trigger <​input id="target" type="text" value="Hello there"> After this code executes, clicks on the Trigger button will also alert the message As the .select() method is just a shorthand for .on( "select", handler ) , detaching is possible using .off( "​select" )  Now that most modern websites use the JQuery library, we should use the correct way of handling click events; This article takes the example of a click triggered onto a row inside a table to highlight the row and load some details inside a field. You may check out the demo to get a glance at the details.

.click(), Description: Bind an event handler to the "click" JavaScript event, or trigger that a shorthand for .on( "click", handler ) , detaching is possible using .off( "click" ) . These methods — including .click (), .focus (), .blur (), .change (), etc. — are shorthand for jQuery's .on () method. The on method is useful for binding the same handler function to multiple events, when you want to provide data to the event handler, when you are working with custom events,

:button Selector, An equivalent selector to $( ":button" ) using valid CSS is $( "button, input[type='​button']" ) . To achieve the best performance when using :button to select elements, first select the elements <input type="button" value="Input Button"> Option. Button. For this type jQuery found 2. Ajax · Global Ajax Event Handlers · Helper  When the first element in the collection is a select-multiple (i.e., a select element with the multiple attribute set), .val() returns an array containing the value of each selected option. As of jQuery 3.0, if no options are selected, it returns an empty array; prior to jQuery 3.0, it returns null.

How To Detect Which Element Was Clicked, Using jQuery, How To Detect Which Element Was Clicked, Using jQuery. By Slavko Pesic element(s). I will describe one of such scenarios below and offer one of possible solutions to it using jQuery. In this case, the element with higher z-index will be returned. Filed under: Footer. Values · Privacy Policy · Careers  In this post you will find out how to get or set style properties/values for selected elements using the jQuery css() method. Sometimes when developing, you may find yourself in a situation where you have to get a CSS property value of a specific element and do something with it.

Comments
  • There is a problem with the finding number. If you change the values like this <input type="hidden" id="multinumber" value="11,22,33,44,55"> <input type="hidden" id="justonenumber" value="3"> and check the number it is also alert 3 is found but there is not have uniq number 3.
  • I see.. In that case you need to split the multiple value. I will update my answer.
  • Do you really need .split? ;)
  • Ah! Not really. it can be skipped. Thanks for pointing though :)
  • Do you really need to .split? ;)
  • removed.. thnx @AdityaParab
  • @mshouman There is a problem with the finding number. If you change the values like this <input type="hidden" id="multinumber" value="11,22,33,44,55"> <input type="hidden" id="justonenumber" value="3"> and check the number it is also alert 3 is found but there is not have uniq number 3.
  • @Azzo I wrote it with splitting it to array but I have changed it so now it's necessary to split the multinumber