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>
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:
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.
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
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.
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');
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');
Try with:
var span = $('<span />').attr('class', 'folder_name').html('Elway');
A jsFiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With