Getting all data binding values ​​using jQuery

function getDbValue() { alert($('[data-bind]').length); alert($('[data-bind][0].data-bind')); alert($('[data-bind][0].value')); jQuery.each($('[data-bind]'), function(databind,key) { alert(key); alert(databind); alert(databind[key].data-bind); }) } 

The above function is my function, and I want to read all inputs that are bound to data properties, e.g.

 <input type="text" id="frmIn1-Officer" data-bind="value: AOfficer" class="InputText"/> 

^ When performing my function, I would like it to return "AOfficer", since this is a data binding value.

So an example

 <input type="text" id="frmIn1-Officer" data-bind="value: AOfficer1" class="InputText"/> <input type="text" id="frmIn1-Officer" data-bind="value: AOfficer2" class="InputText"/> <input type="text" id="frmIn1-Officer" data-bind="value: AOfficer3" class="InputText"/> <input type="text" id="frmIn1-Officer" data-bind="value: AOfficer4" class="InputText"/> <input type="text" id="frmIn1-Officer" data-bind="value: AOfficer5" class="InputText"/> <input type="text" id="frmIn1-Officer" data-bind="value: AOfficer6" class="InputText"/> 

And in every loop, I would like to be able to use the data binding value. for example, the values ​​[0] = 'AOfficer1'

Sorry if my explanation is a bit confusing, I have an idea in my head, but trying to write it down in writing is a lot harder.

+4
source share
5 answers

jQuery interprets data-to-something attributes differently than other attributes. Therefore, you should select all of your elements and look for their data bindings as follows:

 $(document).ready(function(){ $('input.InputText').each(function(){ var input = $(this); if ($(input).data().bind) { alert($(input).data().bind); } }); });​ 

Then you can perform string manipulation to parse your values, I would suggest using JSON and just load it as an object. The fiddle works here: http://jsfiddle.net/3NERK/6/

+5
source

You can search for any element with the data-bind attribute using the jQuery attribute selector - $("[data-bind]") , and then repeat it with .each() and build an array of dataBinds from it, dividing value: from each value .

This will do the trick:

 dataBinds = []; $("[data-bind]").each(function(){ dataBinds.push($(this).attr("data-bind").substring(7)); });​​​​​​ 

I gave an example: http://jsfiddle.net/dvirazulay/YPnwQ/

+2
source
 $( "[data-bind]" ).each( function() { var elem = $( this ); alert( elem.data( "bind" ) ); }); 

http://jsfiddle.net/NhNhK/

+1
source

Get all elements with data-bind attribute: $('[data-bind]')

Iterate over these elements and manage the data binding attribute:

 $('[data-bind]').each(function(element,index){ var data_bind = $(element).data('bind'); alert(data_bind); }) 
+1
source

You can use the .data() method with .each() to accomplish this.

Demo

 $('input').each(function() { var $this = $(this); alert($this.data('bind').replace("value: ", "")); });​ 
0
source

All Articles