Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
var x = '<div><span></span><div id="container"></div></div>'
console.log($(x).find('#container').html())
I wonder why this doesn't work, I just want to extract the html before it's append to somewhere.
There is no html in #container. If you want to get the #container itself then you can do it with outerHTML like following.
var x = '<div><span></span><div id="container"></div></div>';
console.log($(x).find('#container')[0].outerHTML);
jQuery html api is for ( Get the HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element. - http://api.jquery.com/html/)
your code is right, if you want to get "container" html, you have to call other function. e.g) get(0)
$(document).ready(function(){
var x = '<div><span></span><div id="container"></div></div>';
console.log($(x).find('#container').get(0));
});
if you want to add something or object, you can use append API.
$(document).ready(function(){
var x = '<div><span></span><div id="container"></div></div>'; // string
var $x = $(x); //$x is a jQuery object
$x.find('#container').append($('<div>test</div>'));
console.log($x);
});
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I scrape info from webpage and I want to push them to array in order to use them later. But when I try to reach 1st, 2nd etc... item, instead of the word I got back only a character.
var arrType = [];
$('[name="type"]> option').each(function () {
arrType.push($(this).text());
});
const vehicleType = arrType.join(", ");
If I print the vehicleType then I got something what is looks like array (also the typeof is array), but when I want to print out vehicleType[0] I just get back one character.
console.log (vehicleType)
[text1,text2,text3]
console.log (vehicleType[0])
t
First, you can reduce your code a bit. You can define your variable at the same time you pull the option text by using .map() instead of .each().
var arrType = $('[name="type"]> option').map(function() {
return $(this).text().trim();
}).toArray();
Second, to target the first item of the array, don't use .join() at all.
console.log(arrType[0]);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to make a simple code to implement a dynamic source for a image using JavaScript, but without success.
Coding the image path manually it works and the page shows the image.
<script>
function myFunction() {
document.getElementById("myImg").src = "data/3566.JPG";
}
</script>
But when using a variable for dynamic path it shows a X instead of the image.
<script>
let ID = 3566;
function myFunction() {
document.getElementById("myImg").src = "data/ " + ID + ".JPG";
}
</script>
I appreciate any help for checking what I'm doing wrong.
Thanks in advance.
u just have space problem
document.getElementById("myImg").src = "data/" + ID + ".JPG";
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a code here:
$.each(localStorage, function(key, value){
if (key.indexOf('(cache)') > -1) {
var id = key.replace("(cache)", "");
id = '#' + id;
$(id).val(value);
}
});
My target is to update the value of the specific id but it gives an error of unkown expression (expected id here)
and also here is the image of the error
It's highly recommended to convert id properly (don't use dates and spaces). But if you can't then below is a solution
Working snippet:-
myval = 1;
id= '05/03/2018 15:39:51JxM8tX8K';
$('[id="'+id+'"]').val(myval);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="05/03/2018 15:39:51JxM8tX8K"><br>
<input type="text" id="05/03/2018 15:39:51JxM8tX8KDKHFCK">
Note:-I am not fully sure that it will work for you perfectly or not. Test it and let me know
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
So I am using the following jQuery to get a count of all items in HTML with a class containing box. However it is not returning correctly, I feel like I'm missing something stupid.
var counter = $("*[class^=box]").length;
JSFiddle for example: jsfiddle
This link should return 1, but it returns 0.
EDIT:
It was a stupid mistake:I was previously using ID which was only going to be box1,box2,box3,etc so it made sense then. I knew it was stupid.
var counter = $("*[class=box]").length;
The "attribute starts with selector" matches the attribute, not neccesarely the classname
That means that this will match the selector
<div class="box0 emailbodytext" ...
as the attribute starts with "box", while this won't
<div class="emailbodytext box0" ...
as the attribute does not start with "box", even if one of the classes do
Here's one way to solve it
var counter = $('*').filter(function() {
return this.className.split(/\s+/).filter(function(klass){
return klass.indexOf('box') === 0;
}).length;
}).length;
FIDDLE
Your selector basically checks for elements that their class attribute starts with 'box'.
Your have more than one class name in your class attribute so you need to use 'contains' instead of 'starts with'.
Example:
var counter = $("*[class*=box]").length;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a div:
<div id="results"></div>
and in my js:
document.getElementById('results').innerHTML = "foo";
this works correctly however...
I try to store it...
var rslt = document.getElementById('results');
so I can use it more easily. However "rslt" is undefined and in firebug when I mouse over "results" inside the getElementById brackets it doesn't display any info. Like it's a string??
I'm sure this is probably very simple and I just can't see it...
When I call rslt it gives "null" now. But if I remove the "var reslt = " the rest of it "document.getElementById('results')" works perfectly and returns the div.
window.onload = function(){
var rslt = document.getElementById('results');
rslt.innerHTML = "This is working.";
};
Well, if it is undefined that means one simple thing - the object hasn't made it to the DOM tree yet.
Make that call after you're sure the div has been written to the document, e.g. after load event.
It is Working Perfectly..Look at the Fiddle
<div id="results"></div>
document.getElementById('results').innerHTML = "foo";