souparno majumder
souparno majumder

Reputation: 2052

What does it do to invoke jQuery with a tag name wrapped in angle brackets?

I came across the following jquery syntax

var some_variable = $("<input>").attr("type", "some type").attr("name", "some name").val(JSON.stringify(someobj));

I want to know what the $("<input>") syntax is doing? what is the meaning of < , > sign in here?

Upvotes: 2

Views: 88

Answers (5)

Jack A.
Jack A.

Reputation: 4443

It creates an input element.

It http://api.jquery.com/jquery/#jQuery2

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

The syntax in the jQuery object of your example is used to create a new element, in this case an input. Also note that you can set the properties in a single jQuery object instead of chaining multiple attr() calls:

$("<input>", {
    type: 'text',
    name: 'name', 
    value: JSON.stringify({ abc: 123 })
});

Upvotes: 2

Mohit Bhardwaj
Mohit Bhardwaj

Reputation: 10083

That syntax creates a new input element, adds some attributes to it, assigns a value to it. Now, you have an input element which is just not attached to DOM. You can use jQuery's append, appendTo or other insertion methods to attach it anywhere you like in your html DOM.

Upvotes: 0

Quentin
Quentin

Reputation: 943561

See the documentation:

jQuery( html [, ownerDocument ] )
Description: Creates DOM elements on the fly from the provided string of raw HTML.

Upvotes: 1

Rence
Rence

Reputation: 2950

$('<input'>) 

would be used to create a new, non-existing html tag of the type 'input'.

See this for further documentation: http://www.w3schools.com/jquery/jquery_dom_add.asp

Upvotes: 0

Related Questions