3

I need array of elements (inside story) having href attribute starts with view.php.

Here are my tryings, without success:

let arr = $('#story').find(".el[href.startsWith('view.php')]");
//let arr = $('#story').find(".el[attr('href').startsWith('view.php')]");

console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='story'>
<a class='el' href = 'view.php?id=323'>lorem</a>
<a class='el' href = 'about.php'>ipsum</a>
<a class='el' href = 'index.php'>lorem</a>
<a class='el' href = 'view.php?id=525'>ipsum</a>
</div>

4 Answers 4

6

You can use a[href^="view.php"] starts with attribute selector and then get array of href values with map and get methods.

let arr = $('#story a[href^="view.php"]').map(function() {
  return $(this).attr('href')
}).get()

console.log(arr)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='story'>
  <a class='el' href='view.php?id=323'>lorem</a>
  <a class='el' href='about.php'>ipsum</a>
  <a class='el' href='index.php'>lorem</a>
  <a class='el' href='view.php?id=525'>ipsum</a>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

pls help on this way - let arr = $('#story').find(".el[href^='view.php']"); I nedd array of el's
3

document.querySelectorAll('#story > a[href^="view.php"]')

2 Comments

result + some strange text in console, pls try
Yep, it returns an NodeList object ! You can convert it to array easily with [...myNodeList]. Now you should be able to loop over it like a regular array :)
1

This one prints 2 times hello for this situation.

let arr = [];
$('a[href^="view.php"]').each(function() {
    arr.push(this.getAttribute("href"));
});

console.log(arr[0])
console.log(arr[1])
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='story'>
<a class='el' href = 'view.php?id=323'>lorem</a>
<a class='el' href = 'about.php'>ipsum</a>
<a class='el' href = 'index.php'>lorem</a>
<a class='el' href = 'view.php?id=525'>ipsum</a>
</div>

6 Comments

and where is the array wanted ?
Added it for you.
the first one is missing
Ofcourse, I log the second one.Added the first one for you:)
upvoted, but I need array of el's not href's, pls see my comment on Nenad Vracar's post
|
0

Try this:

const links = document.getElementsByClassName('el')

const views = Array.prototype.filter.call(
  links,
  el =>  el.getAttribute('href').startsWith('view')
);

console.log(views)
<div id='story'>
  <a class='el' href = 'view.php?id=323'>First view link</a>
  <a class='el' href = 'about.php'>ipsum</a>
  <a class='el' href = 'index.php'>lorem</a>
  <a class='el' href = 'view.php?id=525'>Second view link</a>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.