How to remove an item from an array? [duplicate] - javascript

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Closed 2 years ago.
I have this
<span #click="remove(product)" class="btn btn-sm btn-danger"><i class="fa fa-times"></i></span> element, It triggers this function:<br/>
remove(product) {
this.form.products.$remove(product);
} <br/>
But it outputs this error:TypeError: this.form.products.$remove is not a function i don't know what is wrong.

Use the splice method to remove the item from an array
First get the index of the item
let index = this.form.products.indexOf(product);
Then remove it using splice
this.form.products.splice(index,1);

Related

How to use getElementById in django [duplicate]

This question already has answers here:
Django Template Variables and Javascript
(18 answers)
Closed last month.
I'm trying to generate a code for editing posts using modal that are made before
but it shows "Identifier or string literal or numeric literal expected" and "Statement expected" ERROR
here is my code:
`
function submitHandler(){
const textareaValue = document.getElementById("textarea_"${post.id}).value
</script>
<button type="button" class="btn btn-secondary"onclick=submitHandler({{post.id}}) > Save Changes</button>
`
want to submit change to editing posts.
if your are sending the textarea id via onclick, so didn't need to get it again via jinja or django template.
<script>
function submitHandler(post_id){
my_textarea = 'textarea_'.concat(post_id);
const textareaValue = document.getElementById(my_textarea).value
</script>
<button type="button" class="btn" onclick=submitHandler({{post.id}})> Save Changes</button>

How to click on a button(no Id in tag) in html page after 5 seconds when page is loaded [duplicate]

This question already has answers here:
How to access HTML element without ID?
(7 answers)
Closed 8 months ago.
A boog noob Alert!...I've html div like this
<div class="col mt-2">
<button #click="selected = Array.from(header); render()" class="btn btn-light-alt btn-sm">Select all</button>
<button #click="selected = []" class="btn btn-light-alt btn-sm">Clear selection</button></div>
All I want to do is click on this Select all button after 5 seconds webpage is loaded. I've searched for this way to do it & I found this
setTimeout(document.getElementByID('button').click(),5000);
I think button is Id here???...But there is no Id in my html tag...we can see only class is available...so can I achieve this requirement?...Any similar threads that you guys know?
If you can't modify the HTML, and that button's function is always the same, then you don't actually need to trigger the click. Just run the functions.
setTimeout(function(){
selected = Array.from(header);
render();
},5000);

Clear a textarea via a button in javascript only [duplicate]

This question already has answers here:
Is "clear" a reserved word in Javascript?
(4 answers)
Why can't I call a function named clear from an onclick attribute?
(3 answers)
How to clear a textbox using javascript
(15 answers)
Closed 4 years ago.
I only know how to use Javascript and HTML5
I have tried so many ways to clear textarea but they won't work
<textarea id="texti" placeholder="Place Text Here Or Paste Text"></textarea>
<button class="button" type="button" onclick="clear()">CLEAR</button>
<script type="text/javascript">
function clear(){
document.getElementById("texti").innerHTML= "";
}
</script>
in the function part I have tried
document.getElementById("texti").value=0;
document.getElementById("texti").value="";
but apparently none of theme works in my code
End your textarea tag
Use .value
Do NOT use reserved words like clear
function clearIt() {
document.getElementById("texti").value = "";
}
<textarea id="texti" placeholder="Place Text Here Or Paste Text">XXX</textarea>
<button class="button" type="button" onclick="clearIt()">CLEAR</button>

jquery encode array for GET queryString [duplicate]

This question already has answers here:
JQuery parameter serialization without the bracket mess
(2 answers)
Closed 5 years ago.
I have an array of id's in JS, like so
var ids = [123,456,789]
I am trying to pass them as part of a querystring in a GET request to a .NET API application. From what I understand .NET needs the querystring to resemble endpoint/ids=123&ids=345&ids=789 or similar.
I've tried using $.param({ ids: ids } ) but it ends up as ids%5B%5D=123&ids%5B%5D=456&ids%5B%5D=789
How can I do this?
Here's my html link to use it. I'm using knockout databinding and observables
<a class="btn btn-sm btn-default" data-bind="attr:{'href':'bill/' + bills().map((data) => {return data.billId;}), 'target':'_blank'}">
<i class="fa fa-print"></i>
Click
</a>
and it currently returns
bill/123,456,789
You could use
ids.map(ids=> `ids=${id}`).join('&')
to create a string with ids=123&ids=345&ids=789.
Aftwards you can just add the concattedIds to you url.
Would be something like this:
<a class="btn btn-sm btn-default" data-bind="attr:{'href':'bill/' + bills().map((data) => `ids=${data.billId}`).join('&'), 'target':'_blank'}">
<i class="fa fa-print"></i>
Click
</a>

Get custom property from a button using Javascript [duplicate]

This question already has an answer here:
Select option base on property key in value
(1 answer)
Closed 6 years ago.
I have this button in HTML , with a property defined by me on it (data-filter-params):
<button class="button" type="submit" data-filter-params="{'Type', 'Filter.Type'},{'Code','Filter.Code'}" >Filter</button>
How can I take with JavaScript\JQuery the value for data-filter-params
plain javascript
var btn = document.getElementsByClassName('button')[0];
console.log(btn.dataset.filterParams)
jQuery
console.log($('.button').data('filter-params'));
This should solve it
var value
$(function(){
value = $('button').attr('data-filter-params')
})
JSfiddle: https://jsfiddle.net/mayank_shubham/3k97gg9k/
It's very easy,
HTML:
<button id="btnSubmit" class="button" type="submit" data-filter-params="{'Type', 'Filter.Type'},{'Code','Filter.Code'}" >Filter</button>
jQuery:
var attr = $('#btnSubmit').attr('data-filter-params');
alert(attr)

Categories

Resources