JavaScript How i may select and sort character in HTML Tag using jQuery?

I have some blocker. How can i select character and sort this inside a tag like <p>-tag in jQuery OR Vanilla JS?

function alphabet(str){
    var x = str.split(" ");
    return x.sort().join(" ");
}
alert(alphabet("foo bar moo"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>foo bar moo</p>
Answer:1

Set the .textContent of <p> element to result of alphabet() call

var p = document.querySelector("p"); 
p.textContent = alphabet(p.textContent.trim());
Answer:2

You can use Node.textContent to get and set an element content.

Your code should look like this:

document.getElementsByTagName("p")[0].textContent = alphabet(document.getElementsByTagName("p")[0].textContent);

Demo:

function alphabet(str) {
  var x = str.split(" ");
  return x.sort().join(" ");
}
document.getElementsByTagName("p")[0].textContent = alphabet(document.getElementsByTagName("p")[0].textContent);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
  foo bar moo
</p>
Answer:3
  1. get DOM element
    JS: var domElement = [depends on element and client];
    https://plainjs.com/javascript/selecting/
    jQuery: var $element = $('selector');
    http://api.jquery.com/category/selectors/

  2. get elements text
    JS: var sText = domElement.innerText;
    jQuery: var sText = $element.text();

  3. set elements text
    JS: domElement.innerText = sText;
    jQuery: $element.text( sText );
    in your case: sText = alphabet( sText );


jQuery

jQuery('p').text( alphabet( jQuery('p').text() ) );
jQuery('selector').text( alphabet( jQuery('selector').text() ) ); // generic

function alphabet(str) {
    var x = str.split(" ");
    return x.sort().join(" ");
}

var $p = jQuery('p');
$p.text( alphabet( $p.text() ) );
alert(alphabet("foo bar moo"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>foo bar moo</p>
Answer:4

I am trying to apply and remove a filter to my active object. My code will apply the filter but will not remove it: $scope.contrastFilter = function () { var obj = canvas.getActiveObject(); ...

I am trying to apply and remove a filter to my active object. My code will apply the filter but will not remove it: $scope.contrastFilter = function () { var obj = canvas.getActiveObject(); ...

I have 2 contenteditable div, i want it that if div (div A) height become greater than 100px move exceed text to another div (div B). I am learning (for example, here div height is greater than 100px,...

I have 2 contenteditable div, i want it that if div (div A) height become greater than 100px move exceed text to another div (div B). I am learning (for example, here div height is greater than 100px,...

  1. fundal height greater than gestational age
  2. div height greater than content
  3. css height greater than 100
  4. row height greater than 409
  5. height is greater than length
  6. fundal height greater than expected
  7. autolayout height greater than
  8. media query height greater than width
  9. css child height greater than parent
  10. excel row height greater than 409
  11. css media height greater than width
  12. if div height greater than scroll
  13. if div height greater than css
  14. if div height greater than
  15. height 100 is greater than parent
  16. height must be greater than 0px
  17. css selector height greater than
  18. css media height greater than
  19. if height is greater than jquery
  20. the height is greater than

I have an angular charComponent that creates pie, and line and other graphs which I want to embed in various places in my app. I use the selector: <chart></chart> To expose a chart. ...

I have an angular charComponent that creates pie, and line and other graphs which I want to embed in various places in my app. I use the selector: <chart></chart> To expose a chart. ...

  1. pass variable angular
  2. angularjs pass variable
  3. pass angular variable to javascript function
  4. pass angular variable to jquery
  5. pass angular variable to html
  6. pass angular variable to onclick function
  7. pass angular variable to php
  8. pass angular variable to javascript
  9. pass angular variable to php function
  10. pass variable to angular component
  11. pass variable component angular 6
  12. pass variable to angular directive
  13. pass variable to angular template
  14. pass variable component angular 2
  15. pass variable to angular controller
  16. pass variable in angular 2 template
  17. pass variable to angular module
  18. pass variable into angular directive
  19. pass variables between components angular
  20. pass variables between components angular 2

I'm very new to javascript and web development. I'm building a page that has multiple areas for users to add and save text in. Each text area has a Button with different ID that I use to toggle the ...

I'm very new to javascript and web development. I'm building a page that has multiple areas for users to add and save text in. Each text area has a Button with different ID that I use to toggle the ...

  1. javascript function that returns a function
  2. javascript function that returns a value
  3. javascript function that returns a promise
  4. javascript function that does nothing
  5. javascript function that returns boolean
  6. javascript function that accepts array
  7. javascript function that returns array
  8. javascript function that returns an object
  9. javascript function that can be suspended and resumed
  10. javascript function that adds numbers
  11. javascript function that returns nothing
  12. javascript function that only runs once
  13. javascript function that calls another function
  14. javascript function that runs on page load
  15. javascript function that executes immediately
  16. javascript function that takes a string
  17. javascript function that adds two numbers
  18. javascript function that returns html
  19. javascript function that takes an array
  20. javascript function that always runs