JavaScript jQuery get part of innerhtml jquery replace part of innerhtml

I need to get the html of a p tag, however there is a br tag inside of it. So it looks like:

<p>
    Some Text
    <br>
    Some More Text
</p>

I need to get the first part and second part separately. So I could do something like:

$('p').html() //Up To '<'
$('p').html() //After '>'

Is it possible?

Answer:1

If you know that there's always going to be a <br> tag, then yes. Otherwise you can utilize regex.

var foo = $('p').html().split('<br>')[0];
var bar = $('p').html().split('<br>')[1];
Answer:2

Try to use .contents() to retrieve the nodes that are present inside,

$('p').contents()[0].nodeValue;
$('p').contents()[2].nodeValue;

And node object has a property called nodeValue, it will give you the text that you need.

DEMO

Answer:3

I can suggest you two options:

First: Using substring (FIDDLE)

var content = $('p').html();
var firstPart = content.substring(0,content.indexOf("<br>"));
var secondPart = content.substring(content.indexOf("<br>") + 4,content.length);

Second: Using split (FIDDLE)

var content = $('p').html().split("<br>");
var firstPart = content[0];
var secondPart = content[1];
Answer:4

Yes, it's possible!!! use the split function...

$('p').html().split("<br>")[0];
$('p').html().split("<br>")[1];
Answer:5
$(document).ready(function(){

   var str = $('p').html();

   console.log(str.split('<br>'));

});
Answer:6

How do you testing angular controller with external library usage such as google analytic event tracking. For example: $scope.showVolumn = function() { ga('send', { 'hitType': 'event', ...

How do you testing angular controller with external library usage such as google analytic event tracking. For example: $scope.showVolumn = function() { ga('send', { 'hitType': 'event', ...

  1. angular testing with jest
  2. angular testing with protractor
  3. angular testing with jasmine
  4. angular testing with cypress
  5. angular testing with selenium
  6. angular testing with karma
  7. angular testing with mocha
  8. angular testing with observables
  9. angular testing with cucumber
  10. angular testing with puppeteer
  11. angular testing with material
  12. angular testing with httpclient
  13. angular testing with router
  14. angular testing with @input
  15. angular testing with services
  16. angular testing with mocks
  17. angular testing with phantomjs
  18. angular testing with headless chrome
  19. angular testing with activatedroute
  20. angular testing with testbed

I'm trying to accomplish the effects on this site, but I only need to move around a few things based on scrolling. My goal is for the basketball to go into the box, then after scrolling down a little ...

I'm trying to accomplish the effects on this site, but I only need to move around a few things based on scrolling. My goal is for the basketball to go into the box, then after scrolling down a little ...

  1. jquery move element to another div
  2. jquery move element up
  3. jquery move element to new parent
  4. jquery move element after another
  5. jquery move element before another
  6. jquery move element position
  7. jquery move element out of parent
  8. jquery move element to top
  9. jquery move element on scroll
  10. jquery move element left
  11. jquery move element to body
  12. jquery move element up one level
  13. jquery move element before sibling
  14. jquery move element to end of container
  15. jquery move element to top of list
  16. jquery move element to end of parent
  17. jquery move element above sibling
  18. jquery move element to top of parent
  19. jquery move element on page
  20. jquery move element above another

I have this jquery code to check for any value on a given input and then add a simple class if the input has a value. But in console i receive error that .val() is not a function? my code is: $.fn....

I have this jquery code to check for any value on a given input and then add a simple class if the input has a value. But in console i receive error that .val() is not a function? my code is: $.fn....

  1. this.value input
  2. this.value input javascript
  3. this value input jquery
  4. this value input text
  5. onchange this.value input
  6. javascript this.value input text
  7. this.value html input
  8. the input value of a function
  9. the input value definition
  10. this closest input value jquery
  11. get this input value jquery
  12. $this- form- input value
  13. $(this) get input value
  14. vue this input value
  15. angular this input value
  16. react this input value
  17. javascript get this input value
  18. js get this input value

I am having a bit of trouble getting momentjs to convert a date to unix in the correct way: var start = new Date(2013,11,2); console.log('Start date: '+start); console.log('Unix date: '+moment(start)....

I am having a bit of trouble getting momentjs to convert a date to unix in the correct way: var start = new Date(2013,11,2); console.log('Start date: '+start); console.log('Unix date: '+moment(start)....

  1. moment js convert unix timestamp to date
  2. moment js convert unix timestamp to utc date
  3. moment js convert unix timestamp
  4. moment js convert unix to date
  5. moment js convert unix
  6. moment js format unix
  7. moment js format unix timestamp
  8. moment js convert to unix
  9. moment js convert to unix time
  10. moment js format unix time
  11. moment convert unix timestamp to date
  12. moment.js unix timestamp to date
  13. moment js unix timestamp to date