javascript get innerHTML of $('#madiv')[i] - javascript

As the title mention i have a jquery request to get an element
but when i do this
cmdState = $('.commande>.entete_commande>.content>#statut')[i]
I can't do cmdState.html() to get the innerHTML
I obtain <div id="statut">Send</div> But what I want is only the html the "Send" char but when I do .html(), I got an error.
Do you have a solution ??
Thanks in advance.

id is a unique
$('#statut').html();
$('.commande>.entete_commande>.content>#statut')[i] will return java script object not jquery , so you can use eq()
$('.commande>.entete_commande>.content>.statut').eq(i);
^^^^use common class here

Related

How to get elementsByClassName and then inside getElementById

Hi I'm rewriting a script from jQuery to pure JS and I don't know how else could i write this.I want to get attribute of element inside class 'form-basket' with id 'przecenajs' I know getElementsByClassName returns object of elements, and that's probably why I get the error:document.getElementsByClassName(...).getElementById is not a function
but I'm not into JS that much so i might be wrong
price = document.getElementsByClassName('form-basket').getElementById("przecenajs").getAttribute("data-procent");
That because getElementsByClassName returns a HTMLCollection object.
You probably want to use querySelector function:
document.querySelector('.form-basket #przecenajs')
console.log(document.querySelector('.form-basket #przecenajs').getAttribute("data-procent"));
<div class='form-basket'>
<div id='przecenajs' data-procent="Hello!">
</div>
</div>
or
document.getElementById('przecenajs')
console.log(document.getElementById('przecenajs').getAttribute("data-procent"));
<div class='form-basket'>
<div id='przecenajs' data-procent="Hello!">
</div>
</div>
Resources
document.querySelector()
Document.getElementsByClassName()
You do not have to select the form-basket first. Since IDs should only be used once inside a document, you can simply selct by id like so:
document.getElementById("przecenajs").getAttribute("data-procent");
I assume you are searching for more than only one tag, because if you wouldn't you could just use document.getElementById() so I think these lines do the job you want, you have to manually create the list with all the attributes to replicate the jquery behaviour:
var priceList = [];
document.querySelectorAll(".form-basket #przecenajs").forEach( (element) =>{
priceList.push(element.getAttribute("data-procent"));
});

I am unable to a inserting the content HTML pre tag

Function objective()
{
Document.getelementbyid("pre").innerhtml=""
}
And in the HTML for my li in nave, I declared the onclick function but it is not working.
Note:- I am clear about case sensitivity in js and using bootstrap3
for resposive.
Use document.getElementsByTagName instead. This will return an array of all the elements that have that tag name.
Due to it returning an array, you need to make sure you use [0] to find the first index in the array.
function objective(){
document.getElementsByTagName("pre")[0].innerHTML="";
}
objective();
<pre>This is some content that will not show in the snippet</pre>
<div>This content will though</div>
getelementbyid() expect a id, not a tag name,your code will call on html like
<pre id="pre">
</pre>
if you want to select elementnot by id but form tag name use document.querySelectorAll("pre")[0] or document.querySelector("pre")
remember querySelectorAll return an array!!
getElementById selects an element with defined id as in your case pre as an id.
You must have an id attached to your element same that of argument in getElementById().
Or, you can use getElementsByTagName which selects element given in argument.

How to get inner html based on attribute Id

I have the attibute Id.
In console when I type in the following jquery command:
$('#LocationRadioButtons a')
I get the following output
[<a id=​"4" href=​"#">Test​</a>​, <a id=​"5" href=​"#">Test1​</a>​, <a id=​"6" href=​"#">test2​</a>​]
Which is an array
If I type in the following jquery command:
$('#LocationRadioButtons a').first();
It will return the first element in that array:
Test​​
How do I return an element based on it's Id, and return its innerHTML. For example id = 5 innerHTML is test1,
Cheers
You can get the html by using html()
You can use
$('#LocationRadioButtons #5').html();
Based off your markup you can actually simply use
$('#5').html();
PS: I'd refrain from having ids start with a number. HTML4 doesn't like this.
while Id is unique for this element you can directly use id to get html
$('#5').html();
Try this,
$('#LocationRadioButtons a[id$="5"]').text();
an id is unique so you can just use the id selector to select an element with a specific id like this:
$('#5').html();
Try this:
As you already have the elements id, just do
$('#5').html();
Will alert Test1
jquery each() loop is useful when you don't have a selector and you want to parse through each element to check for certain condition.
$('#LocationRadioButtons a').each(function(index, value){
var myattr = $(this).attr('id');
if(myattr=='5') {
alert( $(this).html() );
}
});

javascript split the element id from window.getSelection().anchorNode.parentNode.innerHTML

I am getting the following string from javascript innerHTML using this code.
window.getSelection().anchorNode.parentNode.innerHTML;
output is,
<input name="boxes[]" value="checkbox_1" id="box_1" type="checkbox">fgfg
How do i get the check box id from the html string.
i need to find out the id value from the checkbox.
Don't get the innerHTML, get the element you want instead.
window.getSelection().anchorNode.parentNode.getElementsByTagName('input')[0].id

javascript appended to div is coming as a string

I have a java script
$('#linegraph').append(sparkLineData);
where the value of sparkLineData is 1,2,3,4,5,6,7
where
<div id="linegraph"></div>
where in this div i need to append the values like
<div id="linegraph">1,2,3,4,5,6,7</div>
but why is it coming as
<div id="linegraph">"1,2,3,4,5,6,7"</div>
why does it happen like this? It seeems like it is appending as a string.Is there any solution for this to remove the " " ?
Use text() instead:
$('#linegraph').text(sparkLineData);
Also, shouldn't the selector be #sparkLineData?
Use .text()
Try this
$("#linegraph").html(sparkLineData)
Try text()
$('#linegraph').append(sparkLineData);
change to
$('#linegraph').text('Your data');
Try jquery replace power
$('#linegraph').append(sparkLineData).replace('"','');

Categories