Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create span tag and insert with Jquery

Tags:

html

jquery

I am trying to populate a span tag that i was to insert into my page with jquery.

i tried this

var span = $('<span />').attr({'className':'folder_name' , 'text':'favre' });
var insert=
$('<div/>', {
    className: "folder",
    html: span
});

which yielded this result.

<div classname="folder">
<span classname="folder_name" text="favre"></span>
</div>

then i tried this

 var span = $('<span />').attr({'className':'folder_name', 'html':'Elway' });

which yielded this

 <div classname="folder">
 <span classname="folder_name" html="Elway"></span>
 </div>

what i am trying to get is below

 <div classname="folder">
 <span classname="folder_name" >**Elway**</span>
 </div>
like image 571
Frank Visaggio Avatar asked Aug 12 '13 14:08

Frank Visaggio


People also ask

How to change text in a span using jQuery?

To change text in a span using jQuery, the simplest way is to use the jQuery text()method: $("#div1 span").text("Changed Text"); If your span will contain html content, then you should use the jQuery html()method. $("#div1 span").html("Changed <strong>Text</strong>"); Let’s say I have the following HTML:

How to change the anchor content of a tag using jQuery?

Look up html () function in jQuery docs and use it to change anchor content. $ ('a').each (function () { $ (this).prepend ('<span>This is the span content</span>'); }); This will add a span tag to the start of each and every a element on the page.

How to add new content using jQuery?

We will look at four jQuery methods that are used to add new content: append () - Inserts content at the end of the selected elements prepend () - Inserts content at the beginning of the selected elements after () - Inserts content after the selected elements

How do I append elements to text using jQuery?

The elements are created with text/HTML, jQuery, and JavaScript/DOM. Then we append the new elements to the text with the append () method (this would have worked for prepend () too) : var txt2 = $ ("<p></p>").text("Text."); // Create with jQuery txt3.innerHTML = "Text."; The jQuery after () method inserts content AFTER the selected HTML elements.


3 Answers

The HTML of an object is not an attribute and needs to be supplied separately:

var span = $('<span />').attr('className', 'folder_name').html('Elway');

Also, className is not a valid attribute, do you mean class instead, if so use this:

var span = $('<span />').addClass('folder_name').html('Elway');
like image 189
Rory McCrossan Avatar answered Sep 23 '22 03:09

Rory McCrossan


I prefer the 'createElement' method of native JS. This is faster than the jQuery method alone

var $span = $( document.createElement('span') );
$span.addClass('folder_name').html('Elway');
like image 20
Martijn Avatar answered Sep 24 '22 03:09

Martijn


Try with:

var span = $('<span />').attr('class', 'folder_name').html('Elway');

A jsFiddle

like image 44
Daniele Avatar answered Sep 23 '22 03:09

Daniele