search

Home  >  Q&A  >  body text

Edit dynamically created text in Javascript

I'm creating a "Social Media Comments Section" (you can add your own comments by typing in the comment box at the bottom.) I want to add an Edit button that can be changed Click the edit button for any comment. I'm very unfamiliar with how to do this. I tried adding a click function that creates a textbox where you can enter text, but it creates a textbox on every comment that has an edit button. How can I make the Edit button more specific to the comment I clicked on?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

$(document).ready(function(){

    let value;

    let storeValues = []

    let storeValueName;

    let storeValueComment;

    $('#addComment').click(function(){

    storeValueName = getName();

    storeValueComment = getComment();

    storeValues.push(storeValueName);

    storeValues.push(storeValueComment);

    value = getName() + getComment();

    function getName(){

        let grabName;

        $('#name').val(function(i, v){

            grabName = v;

        })

        return grabName;

    }

 

    function getComment(){

        let grabComment;

        $('#bodyText').val(function(i, v){

            grabComment = v;

        })

        return grabComment

    }

    console.log(storeValues);

     

    $('.eachComment').prepend('<div class="comments">' +'<img class="imgClass" src="userImage.jpg">'+'<p class="nameVal">'

    + storeValueName +'</p>' + '<p class="commentVal">'+ storeValueComment +'</p>'+

    '<input type="button" id="edit" value="Edit" />'+ '<input type="button" id="delete" value="Delete" />' + '</div>');

     

        $('#edit').click(function(){

 

    })

     

    })

     

})

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

body{

    background-color: #E5E5E5

}

 

 

 

#wholeForm{

    margin: 0 auto;

    width: 800px;

    height: 400px;

    position: relative;

    background-color: #D3D3D3;

    font-family: helvetica;

}

 

#question{

    padding: 0px;

    margin: 0px;

    width: 780px;

    height: 75px;

    background-color: white;

    padding:10px;

}

 

#nameOfPerson{

  font-size:13px;

  margin: 0;

  padding: 0;

}

 

#commentOfPerson{

  font-size:25px;

  font-weight: bold;

  margin: 0;

  padding: 0;

}

 

.form2{

    width: 800px;

    height: 458px;

}

 

.comments{

    background-color: white;

    width: 780px;

    height: 75px;

    position: relative;

    margin: 10px;

}

 

.form1{

 

    padding:20px;

    margin-bottom: 10px;

    width: 758px;

    position: absolute;

    bottom: -10px;

    background-color: white;

}

 

#addComment{

    display: inline-block;

    margin-left: 35px;

}

 

#name{

    width: 125px;

}

#bodyText{

    width: 500px;

}

 

.formInput{

    display: inline-block;

}

 

.nameVal{

    display: inline-block;

    font-size: 12px;

    position: absolute;

}

.commentVal{

    display: inline-block;

    position: absolute;

    font-weight: bold;

    font-size: 18px;

    bottom: 5px;

}

 

.imgClass{

 display: inline-block;

 height: 65px;

 width: 65px;

 margin: 5px;

}

 

#edit{

    position: absolute;

    right:55px;

    border: none;

    text-decoration: underline;

    color:#30D5C8;

    background-color:white;

    margin:5px;

}

 

#delete{

    position: absolute;

    right:0px;

    border: none;

    text-decoration: underline;

    color:#30D5C8;

    background-color:white;

    margin:5px;

}

 

#edit:hover{

    color:#DDA0DD

}

 

#delete:hover{

    color:#DDA0DD

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<!DOCTYPE html>

<html>

<head>

    <title></title>

    <link href='style.css' type='text/css' rel='stylesheet' />

</head>

<body>

<div id='wholeForm'>

<div id="question">

    <p id="nameOfPerson">WhySoSerious45</p>

    <p id="commentOfPerson">Trying to decide a career path? Programming is the move. Change my mind.</p>

</div>

<div class='form2'>

    <div class='eachComment'>

    <div class="comments">

    <img class="imgClass" src="userImage.jpg">

    <p class="nameVal">Jonny R</p>

    <p class="commentVal">I wish I knew how to program! Maybe ill start learning?</p>

    <input type="button" id="edit" value="Edit">

    <input type="button" id="delete" value="Delete">

    </div>

    </div>

</div>

<div class='form1'>

    <div class='formInput'>

    <input type="text" id="name" placeholder="Display Name"/>

    <input type='text' id="bodyText" placeholder="Comment"></textarea>

    </div>

    <input type="button" id="addComment" value="Submit">

</div>

 

</div>

 

<script src="https://code.jquery.com/jquery-3.5.0.slim.min.js" integrity="sha256-MlusDLJIP1GRgLrOflUQtshyP0TwT/RHXsI1wWGnQhs=" crossorigin="anonymous"></script>

<script src="app.js"></script>

</body>

</html>

P粉268654873P粉268654873377 days ago604

reply all(1)I'll reply

  • P粉593118425

    P粉5931184252024-04-01 10:58:52

    To answer your question in a general sense...

    When you add an event listener, the specific element will be displayed on the event's target attribute. So if we have something like this:

    1

    2

    3

    4

    5

    <div class="posts">

      <button>Button 1</button>

      <button>Button 2</button>

      <button>Button 3</button>

    </div>

    We can actually add a click handler to the div because the event "bubbles" up to it.

    1

    2

    3

    document.querySelector('.posts').addEventListener('click' (e) => {

      console.log(e.target.textContent); // Button 1, Button 2, etc.

    });

    reply
    0
  • Cancelreply