Deleting certain html blocks from HTML String - Javascript or Jquery -


i have html string looks follows :

<a href="#1">1</a> <a href="#a">a</a> <a href="#5">5</a> <a href="#4">4</a> 

i want write generalized function takes integer input. if, example input = 2, function should return html string this

<a href="#1">1</a> <a href="#a">a</a> 

if input parameters = 3

<a href="#1">1</a> <a href="#a">a</a> <a href="#5">5</a> 

and on. suggestions how can achieve ?

try this:

function getfirst(amount, str) {      return $(str).slice(0, amount);  } 

call this:

getfirst(2, '<a href="#1">1</a>' + '<a href="#a">a</a>' + '<a href="#5">5</a>' + '<a href="#4">4</a>') 

will result in:

<a href="#1">1</a><a href="#a">a</a> 

demo: https://jsfiddle.net/24h4lykq/5/


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -