If I use jquery to select all my text inputs:
var inputs = $('#form input[type="text"]');
They are wrapped in jQuery. I can do whatever I want with them.
inputs.css('height', '1000px');
//muhahaha!
As a group they abide. But I seem to be missing something. I know I can see each one individually as if it was an array of objects.
console.log(inputs[0]);
// <input type="text" />
But the above output is just the html; when I do that it's no longer a jQuery object :(
inputs[0].css('font-size', '100px');
// Uncaught TypeError: Object #<HTMLInputElement> has no method 'css'
How do I continue to use jquery's methods on the individual without having to wrap each element again, or is this not possible for some strange, dark, inexplicable reason?
Didn't even know where to begin searching this one, and my jQuery journey has not lead me to the answer thus far. Thanks!
Have a look at this page: jQuery Filtering
inputs.eq(0).css('font-size', '100px');
Should do the trick in this case
see this function .eq() in Jquery-API
When access the Dom-Node-Collection in JQuery via Array-Index, you get the RAW Dom-Elements, if you select the index with the given method above, you'll get feature-enhanced jQuery-Wrapped-Elements.
See docs.
The only way to avoid wrapping each element again($(inputs[0]).css('font-size', '100px'))
would be using pure JavaScript. Example: inputs[0].style.fontSize = "100px";
Related
Firstly, I do know that it is against best practice for multiple elements to have the same ID in a single page. However in this case I need for there to be two selects with the same id.
I have seen some success using this method:
$('#undo_redo_to option').length;
$('#undo_redo_to:eq(0) option').length;
*$('#undo_redo_to:eq(1) option').length;*
However, the code enclosed in *'s does not give me the proper length.
Please see the following pen, where I have created my selects and did my debugging.
LINK TO CODE
Thanks!
The problem is that jQuery uses the native JS functions first, so when you do a $('#this_id') you are effectively calling document.getElementById('this_id'). it's not just against best practice, it actually won't work... you could loop through your selects and check the id:
$('select option').each(function(){
if($(this).closest('select').attr('id')=='my_id'){
//do something
}
});
Assuming you've inherited the HTML and cannot change the ids to classes:
Note that eq() is a jQuery selector. If you change it to nth-child(2), you'll be using a CSS selector. That gives you what you need:
$('#undo_redo_to:nth-child(2) option').length;
http://codepen.io/anon/pen/WwERGW?editors=1011
You can do it with native javascript, like this:
document.querySelectorAll("[id=undo_redo_to]")
You can do it with jquery like this:
$("[id=undo_redo_to]")
This is the property selector, as the #-type selector will stop searching when the first element having that id was found.
So you can do this using plain javascript or jquery, but don't do it, because id should be unique and if it is not unique, then the HTML is invalid and it is bad for SEO.
sorry i am a beginner in jQuery and new in stackoverflow, it is hard for me to tell the differences from those DOM elements.
I want to change the innerHTML in the <p> element, but something strange happened..
For example
html code:
<div id='main-content'>
<p id='p0'>0</p>
<p>0</p>
</div>
js code:
var p=$('#main-content').children('p');
$('#p0').html('100');
p[1].html('100');
I want to change the innerHTML of the first and second element from 0 into 100, but the second method p[1].html('100') doesn't work...and the console said that TypeError: p[1].html is not a function. please help me, Firebug told me that $('#p0') is [object object] and p[1] is [object HTMLParagraphElement], could u explain the differences to me, thank u so much TAT
That's because p[1], just like p.get(1), returns the DOM element.
Use eq to get the jQuery object at index 1 in the set :
p.eq(1).html('100');
jQuery also supports nth-child selectors. So you have that option as well. Fiddle
$("#main-content p:nth-child(2)").text("100");
Like dystroy said, accessing jQuery arrays will return DOM elements. You can use a helper like eq, or simply create another jQuery object by wrapping the DOM element again. So, for example to fix your original code, just do:
var p=$('#main-content').children('p');
$(p[1]).html('100');
Edit: I reread the question, and it seems you want to update content of both paragraph elements. In this case, it becomes even simpler:
$("#main-content p").text("100");
jQuery will act on all selected objects. Please read up on how jQuery uses css selectors
//For First Paragraph with id 'p0';
$('#main-content').find("#p0").html("100");
//For second Paragraph;
$($('#main-content p')[1]).html("100");
I'm new to javascript and jquery, and stumbled upon an issue while writing a script.
My script is generated by php code which reads lines from a file, parses it and prints them out using arrays. js then validates form input, and outputs useful messages to the user.
I have successfully used js and jquery on $('#id').blur on various elements. However when I tried doing it on my indexed element, I came across this problem.
Code:
$('#NS_IN[0]').blur(function() {
alert("Called");
CopyNStoMain();
});
I noticed that this function would never get executed. I tried looking at the variables in console.
typeof($('#NS_IN[0]')) is an object; but typeof($('#NS_IN[0]').val()) is Undefined.
In my html code, I have:
<input type="text" id="NS_IN[0]" value="" name="NS[0]">
What am I doing wrong? If the id NS_IN[0] is defined and $(NS_IN[0]) refers to an object, shouldnt $(NS_IN[0]).val() exist and hold the value of the input box?
You need to escape the jquery selector characters.
$('#NS_IN\\[0\\]').blur(function() {
alert("Called");
CopyNStoMain();
});
You already have the answer here...I don't know how to tag your question as a duplicate.
//get
bla = $('#txt_name').val();
//set
$('#txt_name').val('bla');
In jQuery, the [] works in a different way, like:
div[id^="player_"]
So, one of the solutions, is to select the items which ID starts with something:
$("input[id^=NS_IN]").val();
It works when you use a different selector, as jquery uses the [] as an attribute selector itself. So use e.g. (see fiddle: http://jsfiddle.net/rePQm/1/ ):
$('input').click(function() { alert("clicked " + this.id); });
an element selector that selects all input elements and adds the click handler to all of them. See the selectors section of the jquery documentation at http://api.jquery.com/category/selectors/ for more possible selectors .
I know how to do the opposite. Getting a certain DOMElement for a jQuery element is easy. (Use the get() method)
But how can you get a jQuery element for a specific DOMElement?
Unfortunately this DOMElement does not have any attributes like class or id so constructing a selector is not really an option.
Lets say I have this html:
<div class="edit">Abcd<b><i><u>asdasd</u>adasda</i></b>sdfsdf<br>asd</div>
I am in the u-DomElement. How can I get this as a jQuery element?
Is there a smart way to do this?
EDIT:
I wanted to know if there is a gerneral way to do this. Not specific to the code shown above.
Like:
DomElement.toJQuery()
Is there anything like that? I am aware that this might not be possible.
Getting a jQuery object for a DOM object is as simple as jQuery(dom_node) (or $(dom_node)). See http://api.jquery.com/jQuery/
This is commonly used in event handlers, which are given the DOM node as this, so that you will often see $(this)
If you want to get just the Element use the below code. if you wanted to get the HTML of any element you might want to add the .html() tag to either of the examples
var myVar = $('.edit u');
or
var myVar = $(".edit").find("u");
Are you looking for this?
$(".edit").find("u");
hope this is what you are looking for,
$(DomElement)
you want a only 1 specific dom element i suggest you find a way to add an id to that element.
but to get an u element inside a edit class:
$('.edit u');
$('.edit').find('u');
How can I get jQuery to return a html element exactly the way that getElementById does?
if I console.log($('#container')) I will get :
[<div id="container"></div>]
But the API I am interacting with obviously doesn't want that (an array) - it wants :
<div id="container"></div>
I know I can achieve this with $('#container').get(0) and a few other ways, It just seems overkill - especially as I'm selecting an ID which will always be unique.
What am I missing guys?
Thanks
If you just want a single DOM element, then just use plain javascript:
var obj = document.getElementById("container");
jQuery selectors always create a jQuery object with an array of elements in it. That's just the way it's designed. If you want a single element, you either get the first element out of the jQuery object or you use a different tool.
From a jQuery object, you can get the first object either with:
$('#container').get(0)
or with:
$('#container')[0]
But, I would argue that both are more than you need if all you want is the single object that has an id. Just use document.getElementById(). If you want less typing, you could make your own short function:
function $$(id) {
return(document.getElementById(id));
}
var obj = $$("container");
try using .html() it will return the html of the element your selecting see:
http://api.jquery.com/html/