How to set the content of an element to value of its attribute with jQuery

dlite922

This is just an exercise that I thought of while playing with jsFiddle.

Given elements with a data attribute:

<div data-test-id="3214581">Loading...</div> <div data-test-id="6584634">Loading...</div>

I want to set the text content to result of function with that ID, such that final DOM is:

<div data-test-id="3214581">John, Smith</div>

so far I was able to find the given element and but somehow can't refer to the element using this keyword to get its testId:

$('div[data-test-id]').text(getPersonName($(this).data('testId')))

getPersonName() returns "John, Smith"

I think it should be that simple, but haven't found a self-referencing example like this on stack or jQuery docs.

EDIT: Fixed element to show multiple divs, not just one. (i.e. the ID is not known and shouldn't be in selector). Fixed select to have single quotes around it.

moonwave99

this is the context of the function.

If nothing specifies further, this is simply window.

What you want is to pass a callback to $.text, that will be bound to the selected DOM element:

$('div[data-test-id]').text(function(){
  // here this is your div
  return getPersonName($(this).data('testId'))
})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set the content of an element to value of its attribute with jQuery

From Dev

How to set attribute to element from its content?

From Dev

Jquery, how to select an element by its first position value of an attribute

From Dev

How to change a element/tag value by its attribute value?

From Dev

How to get an element by its style value in Jquery

From Dev

How to iterate through the element attribute value in jquery?

From Dev

Java: how to set default value to annotation with another annotation as its attribute?

From Dev

XSL transformation - how to set attribute value to element value

From Dev

How to use an element's attribute value to target another element with an identical attribute value in jQuery?

From Dev

How can set javascript variable value as id attribute of a html element

From Dev

jQuery : How to return an element by the value of a class of its child

From Dev

jQuery : How to return an element by the value of a class of its child

From Dev

How to set the content property of an `after` pseudo-element to an attribute of a sibling form element

From Java

jQuery how to find an element based on a data-attribute value?

From Dev

How to find the nearest parent element attribute value using Jquery

From Dev

jQuery: How retain the value of the attribute on an element after dropping using sortable

From Dev

How to modify value attribute and class of a HTML element with jQuery?

From Dev

jQuery set attribute without value

From Dev

Set value to attribute using jquery

From Dev

How to set attribute selected true for option with no value in jquery

From Dev

How can I select an element by an attribute set on its parent using CSS 2.0 selectors?

From Dev

How can I select an element by an attribute set on its parent using CSS 2.0 selectors?

From Dev

Set content value of itemprop with jQuery

From Dev

How do I rename an XML element with the value of its first attribute using XSLT?

From Dev

How do I rename an XML element with the value of its first attribute using XSLT?

From Dev

how to return element attribute value

From Dev

How to find a value of an attribute of an element

From Dev

How to get an attribute of element by jquery

From Dev

JQuery: Set attribute for nth element by name selector

Related Related

  1. 1

    How to set the content of an element to value of its attribute with jQuery

  2. 2

    How to set attribute to element from its content?

  3. 3

    Jquery, how to select an element by its first position value of an attribute

  4. 4

    How to change a element/tag value by its attribute value?

  5. 5

    How to get an element by its style value in Jquery

  6. 6

    How to iterate through the element attribute value in jquery?

  7. 7

    Java: how to set default value to annotation with another annotation as its attribute?

  8. 8

    XSL transformation - how to set attribute value to element value

  9. 9

    How to use an element's attribute value to target another element with an identical attribute value in jQuery?

  10. 10

    How can set javascript variable value as id attribute of a html element

  11. 11

    jQuery : How to return an element by the value of a class of its child

  12. 12

    jQuery : How to return an element by the value of a class of its child

  13. 13

    How to set the content property of an `after` pseudo-element to an attribute of a sibling form element

  14. 14

    jQuery how to find an element based on a data-attribute value?

  15. 15

    How to find the nearest parent element attribute value using Jquery

  16. 16

    jQuery: How retain the value of the attribute on an element after dropping using sortable

  17. 17

    How to modify value attribute and class of a HTML element with jQuery?

  18. 18

    jQuery set attribute without value

  19. 19

    Set value to attribute using jquery

  20. 20

    How to set attribute selected true for option with no value in jquery

  21. 21

    How can I select an element by an attribute set on its parent using CSS 2.0 selectors?

  22. 22

    How can I select an element by an attribute set on its parent using CSS 2.0 selectors?

  23. 23

    Set content value of itemprop with jQuery

  24. 24

    How do I rename an XML element with the value of its first attribute using XSLT?

  25. 25

    How do I rename an XML element with the value of its first attribute using XSLT?

  26. 26

    how to return element attribute value

  27. 27

    How to find a value of an attribute of an element

  28. 28

    How to get an attribute of element by jquery

  29. 29

    JQuery: Set attribute for nth element by name selector

HotTag

Archive