0

I want to change the content of a button when clicked - specifically I want it to change to a "block" state where it can't be clicked, while I want to give it a Glyphicon.

What I want added:

<span class="glyphicon glyphicon-saved" aria-hidden="true"></span>

and the text "Downloaded" - I wish for this to erase the previous text and Glyph icon, and I've found this little peace of jQuery that changed the text on click.

<script>
$(document).ready(function(){
$('#download-btn').click(function() {
 $("#download-btn").text('Downloaded');
})
});    
</script>

but it wouldn't allow me to add any HTML code, it just outputted it as pure text.

And last, I want to add the following style btn-block to the button.

This is the button before being clicked;

<button id="download-btn" type="button" class="center btn btn-primary">
    <span class="glyphicon glyphicon-save" aria-hidden="true"></span> Download
</button>

After click, the jQuery above changed the text to "Downloaded", but I can't seem to figure out how I add style, and code, to a tag.

2 Answers 2

1

With jQuery:

There are many more jQuery functions for DOM manipulation.

I'd suggest taking a look. jQuery API

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

Comments

1

Use .html() to apply/override HTML that is inside the element you are selecting.

You can disable buttons in a few ways. I used .attr() or you can use .prop().

$('#download-btn').click(function() {
   $(this).html('<span class="glyphicon glyphicon-ok"></span> Downloaded').attr('disabled', true); 
});

jsFiddle Example

1 Comment

Press, a suggestion -- I would add the markup for the button in the snippet or just change it to a standard code block, it's a bit deceiving when you click the run snippet button and nothing shows up.

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.