1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

overwrite text when clicking on button multiple times.

Discussion in 'jQuery' started by iago111, Apr 18, 2020.

  1. #1
    Hello,

    I have buttons ("+")(green) for different sections, when I click on one of these buttons, I open an input field and a textarea field. The data of both fields should be finally put into a database, with foreign key of each section.

    Link: http://science-travel-com.stackstaging.com/cpicks/

    I show the section text in the label of the headline field, so that the user knows to which sections the new input data belongs to.

    This works well, however with every click on a "+"-Button a new text is added, but the old text not removed. How can I remove the old text and add the new one?

    Here is the function:

          
    
    $('.plus').on('click',function(e){
            
            $('.newCP').show();
            $('.sectionInput').hide();
             $('.updateCP').hide();  
              e.stopPropagation();
           
            $('.labelTextareas').show();
            $('.myTextareas').show();
        
            newCpId = $(this).parent(this).attr('id');
            
    
         //HERE THE SECTION TEXT IS ADDED, HOWEVER CANNOT BE 
         // OVERWRITTEN WHEN "+" BUTTON IS ACTIVATED AGAIN
           $('label.headlineNewCp').before('&nbsp;for&nbsp;<b>' + $(this).siblings('span').text() + '</b>,&nbsp;');
                
    
            });
          
           $('#newCP').on('click', function() {
              
               $.ajax({
              //....for db                
                 }
             });
              
           }); 
    Code (JavaScript):
    Thanks, guys!
     
    iago111, Apr 18, 2020 IP
  2. iago111

    iago111 Member

    Messages:
    99
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    33
    #2
    I thought about empty():

    
    $('label.headlineNewCp').empty().before('&nbsp;for&nbsp;<b>' + $(this).siblings('span').text() + '</b>,&nbsp;');
    
    Code (JavaScript):
    However, this does only remove the text, that has not been generated by the anonymous function. (The text that has been there before.)
     
    iago111, Apr 18, 2020 IP
  3. iago111

    iago111 Member

    Messages:
    99
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    33
    #3
    G.. d...., sorry :)

    it's:
    $('label.headlineNewCp').html(str);
    Code (JavaScript):
     
    iago111, Apr 19, 2020 IP