1

I am using jquery sortable library where i will drop item from one list to another.In the process of dropping item i will change html content.I have data attribute also.

onAdd: function (evt) {            
  var df="hm "+evt.item.innerText;
  alert(df);
  var content='<div class="list-group-item " >'+evt.item.innerText+' its modified  <span class="badge badge-primary badge-pill float-right" data-job='+df+'>14</span></div>';

  $(evt.item).replaceWith(content);

},

in alert i get correct appended value but in data-job it will set only hm and evt.item.innerText will not append

Can any one help me to append it properly.

Thank you

3
  • 4
    Try data-job="'+df+'" quotes for attribute. Otherwise your code seems correct Commented Nov 18, 2019 at 13:31
  • @murli2308.add it as answer i will accept it Commented Nov 18, 2019 at 13:40
  • 3
    You'd be better off just deleting the question as it was a typographical mistake Commented Nov 18, 2019 at 13:41

2 Answers 2

1

You are missing quotes in the expression

Use below code data-job="'+df+'"

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

Comments

1

Since you have space between the text, the text after the space is treated as another attribute of the element. You should wrap the text with quotes data-job="'+df+'".

Though I prefer using Template literals which is more cleaner and easier to use:

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. They were called "template strings" in prior editions of the ES2015 specification.

Demo:

function test(evt) {
  var df="hm "+evt.target.innerText;
  var content=`<div class="list-group-item">evt.target.innerText its modified  <span class="badge badge-primary badge-pill float-right" data-job='${df}'>14</span></div>`;
  console.log(df);
  console.log(content);
  $('body').append(content);

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div onclick="test(event)">test</div>

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.