Thursday, December 18, 2014

Finding all elements whose Id starts with a specific word using jQuery

Some time we come across with a problem where we have a lots of elements in HTML or JSP page which is generated dynamically and their ID starts with some special word.
Like as follows


<input id="item_1fruit" name="fruit" type="text"  value=" Mango" />
<input id="item_2veg" name="vegetable" type="text" value="Potato" />
<input id="item_3eggs" name="eggs" type="text" value="Eggs" />
<input id="item_4nuts" name="nuts" type="text" value="Almonds"/>

So above we can get all elements in jQuery as follow

$("[div^=item_]")   // do whatever you want
 In below code we are iterating all the elements and setting some values.

$("[div^=item_]").each(function(){

     var text_value= $(this).val();
      alert("Text box value "+text_value);
        });


No comments:

Post a Comment

Spring Boot Config Server and Config Client.

 In Spring cloud config we can externalise our configuration files to some repository like GIT HUT, Amazon S3 etc. Benefit of externalising ...