Nickool
Nickool

Reputation: 3702

how to change the text in element via ajax?

I have a button of download,when clicked it should change the text of the element span but I have problem in ajax part

after clicking it will show me a little button and the text will not change

here is my html

//somecode
    <span class="num">Downloaded:<?php echo $downloadcount;?></span>
     </td></tr>
     </table>
     </div>
<button type = "button" class="button" id="<?php echo $id; ?>" name="dl">
    <a href='./mp3/<?php echo basename($filename);?>'>Download</a>
</button>
         </td>
     </tr>
    </table>

here is my ajax:

    $(function() {
        $(".button").click(function() 
        {
        var id = $(this).attr("id");
        var name = $(this).attr("name");
        var dataString = 'id='+ id ;
        var parent = $(this);
        if(name=='dl')
        {
        $(this).fadeIn(200).html('<img src="dot.gif" align="absmiddle">');
        $.ajax({
           type: "POST",
           url: "download_number.php",
           data: dataString,
           cache: false,

           success: function(html)
           {
//THE PROBLEM IS HERE
            $(".num").text(html);

          }  });

        }
        });
        });

I also tried $(".num").html(html);

Upvotes: 1

Views: 1560

Answers (2)

moonwave99
moonwave99

Reputation: 22817

You shouldn't put an <a> inside the <button> - the link will win over the button action [see fiddle].

Beyond this, your code seems right.

Upvotes: 2

Ankush Jain
Ankush Jain

Reputation: 6999

change datastring to this

var dataString = "{'id'="+ id+"}" ;  

and add datatpe json...By doing so it may work....just guessing

datatype:'json', 
  success: function(html)
               {
               alert(html);
               // First see...what u r getting in response and then move forward

              } 

Upvotes: 0

Related Questions