How do we convert jQuery $('div#item').html() in VanillaJS [duplicate] - javascript

This question already has answers here:
How can I use innerhtml in JavaScript?
(4 answers)
Closed 5 years ago.
I need to convert jQuery method to VanillaJS
$('div#item').html()
can convert .html() using innerHTML. But have the issue with $('div#item'). I used document.getElementById('div#item').innerHTML It's not working for me. Someone please suggest any better solution.

just use the actual id of the element you want to acess
document.getElementById('item').innerHTML

Try
document.getElementById('item')

You may use vanilla JS for this task
document.querySelectorAll('div#item')[0].innerHTML;

try this:
console.log (document.querySelector('div#item').innerHTML)
<div id="item">ololo</div>

Related

How to use custom variable in JavaScript [duplicate]

This question already has answers here:
Javascript getElementById based on a partial string
(9 answers)
Closed 3 years ago.
I just want to know to how to use custom varible with javascript.suppose we have 5 id with name zawsz,baws2,tawsx,paws4,vawsa and we want to execute single DOM command to all these element whose id is define here.
document.getElementById("-aws-").onclick=function(){}
Here -aws- define all the id define above.(-) can be replace with any char/int value;
You could use the following code:
(The following code will select all elements of which the id includes aws.
I have tested this code and it works: https://jsfiddle.net/5042woqz/)
document.querySelectorAll('*[id*="aws"]').forEach(function(item) {
item.onclick=function() {
console.log('click!');
};
});
Items will now be an array containing all your aws- items.
If you have further questions, just let me know.
P.S.: You could achieve the same thing really easily with jquery.
You can use document.querySelectorAll for this:
document.querySelectorAll('[id^="aws"]')
That will select all elements where the id attribute starts with (^=) "aws".

Why can I not find using data attribute [duplicate]

This question already has answers here:
jQuery Data vs Attr?
(3 answers)
Closed 5 years ago.
In code I do this:
var markerName = $(fileInput).closest('tr.file-input-row').find('input[type="text"]')[0].value.replace(/[^a-z0-9]/gi, '-');
$.data(fileInput, 'for', markerName);
in this case, markerName is "file-1"
If I check using:
$('input[type="file"][data-for="file-1"]')
I get an object with length equal to 0... so not found.
However, if I do:
$('input[type="file"]:first').data().for
in this case, the first input[type="file"] is the same input I set the data attribute for, I get:
"file-1"
...as expected.
It looks like it is being set, but it is then not accessible.
Any thoughts why?
TIA
This is not working because you aren't updating the DOM object when you perform the data-for adjustment in the first part of your code. To update the DOM object you should use attr(key, value).
For more info on the differences between data and attr there is a good answer related to this: jQuery Data vs Attr?
If I check using:
$('input[type="file"][data-for="file-1"]')
Neither .data() nor jQuery.data() is part of the object returned by jQuery() : $() call.

How to include a PHP variable in Javascript code? [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 5 years ago.
[1] http://imgur.com/a/Ny8f7 "html code"
[2] http://imgur.com/a/kzEN2 "php code"
I try to use the variables from php in javascript to generate a chart with them.
Try using an id e.g.
In your html
name='someName'>
Then use that ID in your JS.
Use can use something like this:
var data = '<?=$dataFromPhp?>';

jQuery / Ajax simple chatbot, how ignore lowercases [duplicate]

This question already has answers here:
How to do case insensitive string comparison?
(23 answers)
Closed 7 years ago.
I am currently programming a simple chatbot but I getting trouble with the input identifier.
if (message.indexOf("how are you")>=0){
send_message("Thanks, Iam good!");
responsiveVoice.speak("Thanks, Iam good!");
Is person is tying in "How are you" the answer will not be triggered since its not lower case. I could copy/paste if state with "How" but is there a easier way? Please be gentle, aim new to programming. Thank you!
Use .toLowerCase()
if (message.toLowerCase().indexOf("how are you") >= 0)
You could try toLowerCase function of javascript.
messaje.toLowerCase().indexOf("how are you")...
Function:
http://www.w3schools.com/jsref/jsref_tolowercase.asp

qts on javascript, jquery [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Request Address in Javascript
Is there any method to retrieve the URL of the current page in JavaScript or jquery?
Please help.
window.location.href
Here is how you would do it in jQuery :P
(function($) {
$.getUrl = function() {
return window.location.href;
}
})(jQuery);
jsFiddle.
Warning: Obviously the jQuery function is for LOLs. Don't really use it.
Yes, use location.href
alert(location.href)
Use the following
document.location.href
Use the
document.location.href

Categories

Resources