0

I have buttons with labels that change depending on the topic that gets loaded. When clicked, the button loads new data to a chart. This works well on span elements but it doesn't work when I wrap the span in a button.

Example:

<button type = "button" class = "btn btn-default"><span id="cat1"></span></button>

Labels are set with :

click: function (e) {
            document.getElementById('cat1').innerHTML = 'something'
};

Data is bound on click with:

$("#cat1").click(function() {
   // do stuff
 });

something gets lost when I add the button for the span. I have tried to replace span with an anchor but it doesn't work either.

Monduiz
  • 711
  • 10
  • 22

4 Answers4

1

Did you try?

<button type="button" class="btn btn-default" id="cat1"></button>
1

How you are using jQuery, you can try a more simple form.

Try it:

$('#cat1').click(function () {
    $(this).html('something');
});

Or if the set value is out of the event, you cant try it:

$('#cat1').html('something');
David Thomas
  • 249,100
  • 51
  • 377
  • 410
Rafael Botelho
  • 398
  • 5
  • 11
  • Rafael, interesting syntax. I need to have the content defined in advance though not just on click so its setup a little earlier in the app. – Monduiz Jun 03 '16 at 19:57
0

You can update the text in span when button is clicked using following code

$('#btnClick #cat1').text("something");

In the above I gave a id for button as btnClick.

You can achieve the functionality without using span as well. For that you need to specify button as

<button id="btnClick">Button</button>

To change text of button, the code which need to be in click event will be

$('#btnClick').text("change")
0

When a span is nested inside a button, every time you click on it, the button click is fired. but the span click is not fired

based on your html, it looks like you are using bootstrap, you can do something like the following

<div class="btn btn-default">
    <span id="cat1"></span> 
</div>

I have a working example in the fiddle.

http://jsfiddle.net/pparas/dv3amy7k/1/

pparas
  • 549
  • 4
  • 13