0

I want to do addClass in jQuery,

When i do try below codes, it result me like image 1

 $('#contineButton').html('Continue').addClass('fa fa-chevron-right');
 $('#contineButton').html('Continue').after().addClass('fa fa-chevron-right');

enter image description here

but, iwant to get the result like below image 2

enter image description here

Original button code:

<button type="button" class="btn btn-success next-step" id="contineButton">
                ${vo.continueButtonText} <i class="fa fa-chevron-right"></i>
</button>
Yoga
  • 293
  • 3
  • 8
  • 23
  • What does your HTML and CSS look like? Either way, `after()` is not what you need as that's designed to insert content after the selected element – Rory McCrossan Jan 20 '17 at 12:13
  • Is the *Original button code* the output of your `addClass` operation or what you are trying to achieve? – Kilian Stinson Jan 20 '17 at 12:16
  • 2
    You just replaced your _whole_ button content with the text `Continue` - so the `i` element does not even exist any more. – CBroe Jan 20 '17 at 12:17
  • 1
    You probably want `$('#contineButton').html('Continue ');` – connexo Jan 20 '17 at 12:17

5 Answers5

1

You're using after() and you should be using append()

 $('#contineButton').html('Continue').append('<i class="fa fa-chevron-right"></i>');

EXPLANATION:

You used html->addClass wich in the first try will add the class to the current object (the button) and in the second try (with after) will attempt to do it in an empty element inserted afterwards. The html replaced the content of the button completly so the <i> didn't existed anymore.

LordNeo
  • 1,195
  • 1
  • 8
  • 21
1

Try this one:

document.getElementById('contineButton').insertAdjacentHTML('beforeend', '<i class="fa fa-chevron-right"></i>');

But it can be achieved with CSS.

eric.dummy
  • 399
  • 1
  • 8
  • 24
  • Thanks. It may be out of this topic, but I don't understand why are people trying to achieve these trivial tasks with jQuery. – eric.dummy Jan 20 '17 at 12:41
1

To add text before tag you need to use .before jquery function:

$('#contineButton i').before('Continue');

For your case you need replace text with new text string:

$('#contineButton').contents()[0].remove();
$('#contineButton i').before('Continue');
Banzay
  • 9,310
  • 2
  • 27
  • 46
0

EDIT: This only works incase you're using Bootstrap

Is there a reason why you are trying to achieve this using jQuery?

Should work with this html/css code.

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<button type="button" class="btn btn-success btn-lg">
  Button <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
</button>
Jeroen
  • 1,168
  • 1
  • 12
  • 24
0
<button type="button" class="btn btn-success next-step" id="contineButton">${vo.continueButtonText} <i class="fa fa-chevron-right"></i>
</button>



$(document).ready(function() {
   $('#contineButton').contents().filter(function() {
                console.log(this)
            return this.nodeType == 3;
        })[0].nodeValue = "The text you want to replace with"
});

JSfiddle

Pravin W
  • 2,451
  • 1
  • 20
  • 26
  • more info can be found here - http://stackoverflow.com/questions/4106809/how-can-i-change-an-elements-text-without-changing-its-child-elements – Pravin W Jan 20 '17 at 12:28