2

$(document).on('click', '.atitle', function(){
  //for each data attr - corresponding input value = data.value
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='atitle' data-x='lorem' data-y='ipsum' data-z='dolor'>title</div>
<br>
<input type='text' id='input_x'>
<input type='text' id='input_y'>
<input type='text' id='input_z'>

result:
input_x has value lorem
input_y has value ipsum
input_z has value dolor

6
  • what is the relationship between data attributes and input id values? Commented Jun 13, 2020 at 22:51
  • data-* and input_* ? Commented Jun 13, 2020 at 22:51
  • @TomaszBucko, yes, of course Commented Jun 13, 2020 at 22:53
  • and array will be only x, y, z? or more strings? Commented Jun 13, 2020 at 22:56
  • @TomaszBucko in reality there is 14 data attributes, so I need a loop Commented Jun 13, 2020 at 22:57

4 Answers 4

2

$(document).on('click', '.atitle', function() {
  // get data of all data attributes 
  // (it's an object like this { x: "lorem", y: "ipsum", ...)}
  let data = $(this).data();
  // Set data to input values.
  Object.entries(data).forEach(([key, value]) => {
    let elem = $(`#input_${key}`);
    // Check if elem exists before trying to assign a value. 
    // Just in case if .atitle contains some data attributes, 
    // which have no mapping to an input element.
    elem && elem.val(value);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='atitle' data-x='lorem' data-y='ipsum' data-z='dolor'>title</div>
<br>
<input type='text' id='input_x'>
<input type='text' id='input_y'>
<input type='text' id='input_z'>

Sign up to request clarification or add additional context in comments.

3 Comments

elem && elem.val(value); - can you explain, pls. btw I didn't downvote
@qadenza The second part of the expression is only executed if elem is set. It's a short way for if (elem) elem.val(value) Check also the comment.
@qadenza Instead of elem && elem.val(value); you could actually also use elem?.val(value); But you need to be carefully, since some mobile browsers do not support it yet: developer.mozilla.org/de/docs/Web/JavaScript/Reference/…
1

you can read this

$(document).on('click', '.atitle', function(){
  var title= $(".atitle");
  let arr=title.data();
  console.log(arr)
  $('input').each(function( index ){   
      $( this ).val(arr[this.id.split("_")[1]])
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='atitle' data-x='lorem' data-y='ipsum' data-z='dolor'>title</div>
<br>
<input type='text' id='input_x'>
<input type='text' id='input_y'>
<input type='text' id='input_z'>

1 Comment

This won't work, if there are additional or maybe missing inputs or if the order of the input differs.
1

please take a look at this example

const trigger = document.querySelector(".atitle");

trigger.addEventListener("click", clickHandler);

function clickHandler(event) {
  const { x, y, z } = Object.assign({}, event.target.dataset);

  document.querySelector("#input_x").value = x;
  document.querySelector("#input_y").value = y;
  document.querySelector("#input_z").value = z;
}
<div class="atitle" data-x="lorem" data-y="ipsum" data-z="dolor">title</div>
<br />
<input type="text" id="input_x" />
<input type="text" id="input_y" />
<input type="text" id="input_z" />

See

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

2 Comments

Why do you want to use const { x, y, z } = Object.assign({}, event.target.dataset);. You could just use const { x, y, z } = event.target.dataset;
Nice trick, The answer is because it makes explicit my intention. btw here another way const {x, y, z} = {... event.target.dataset};
1

Please check following code. It's fully independent from keywords of attributes.

$(document).on('click', '.atitle', function(){
  var title= $(".atitle");
  $.each($(title)[0].attributes, function(key, value) {
    var attr_name = value.name
    if (attr_name.indexOf('data-') > -1) {
        var attr_val = value.value
        var attr_idx = attr_name.split('data-')[1]
        $('#input_' + attr_idx).val(attr_val)
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='atitle' data-x='lorem' data-y='ipsum' data-z='dolor' data-xx='xlorem' data-yy='yipsum' data-zz='zdolor'>title</div>
<br>
<input type='text' id='input_x'>
<input type='text' id='input_y'>
<input type='text' id='input_z'>
<input type='text' id='input_xx'>
<input type='text' id='input_yy'>
<input type='text' id='input_zz'>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.