Skip to content
Advertisement

How do I add text to specific div element using jQuery?

I am having a problem with jquery. My HTML is

JavaScript

Now I am trying to add text in span using Jquery.

JavaScript

After that it becomes,

JavaScript

But, I am tryting to achieve is,

JavaScript

How can I do that in jquery?

Advertisement

Answer

Several possible alternatives

Others have provided their answers already but let me give you some more alternatives.

JavaScript

The second and last one likely being the fastest because they target particular container by ID.
(internal jQuery optimisations may prove me wrong with any future version)

Performance comparison

Here’s a JSPerf test that performance compares upper possibilities. As anticipated, the second and last approaches are the fastest of all because element selection is much simplified. I’ve tried running them on Chrome and you may run them in other browsers if you want to and see differences.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement