how to change an element type using jquery
Here's one way you could do it with jQuery:
var attrs = { };
$.each($("b")[0].attributes, function(idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
});
$("b").replaceWith(function () {
return $("<h1 />", attrs).append($(this).contents());
});
Example: http://jsfiddle.net/yapHk/
Update, here's a plugin:
(function($) {
$.fn.changeElementType = function(newType) {
var attrs = {};
$.each(this[0].attributes, function(idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
});
this.replaceWith(function() {
return $("<" + newType + "/>", attrs).append($(this).contents());
});
};
})(jQuery);
Example: http://jsfiddle.net/mmNNJ/
Changing an element type using jquery
How about, instead of:
$("#file01b").replaceWith(function () {
You use:
$("#"+e.target.id).replaceWith(function () {
Check your fiddle, updated.
Note: If the above is fine, then $(e.target).replaceWith(function () {
will work as well.
change type of input field with jQuery
It's very likely this action is prevented as part of the browser's security model.
Edit: indeed, testing right now in Safari, I get the error type property cannot be changed
.
Edit 2: that seems to be an error straight out of jQuery. Using the following straight DOM code works just fine:
var pass = document.createElement('input');
pass.type = 'password';
document.body.appendChild(pass);
pass.type = 'text';
pass.value = 'Password';
Edit 3: Straight from the jQuery source, this seems to be related to IE (and could either be a bug or part of their security model, but jQuery isn't specific):
// We can't allow the type property to be changed (since it causes problems in IE)
if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode )
throw "type property can't be changed";
Change element type, preserving jQuery event handlers
I found another question that seems to answer what you're trying to do
Duplicate one element's event handlers onto another?
They do the following
$.each($._data($('#original').get(0), 'events'), function() {
// iterate registered handler of original
$.each(this, function() {
$('#target').bind(this.type, this.handler);
});
});
Accessing the $._data
of the original element $('#original')
allows you to gain access to the events it has attached, so iterating over each one allows you to attach the same events to a new element, in this case $('#target')
Use jQuery to change an HTML tag?
Once a dom element is created, the tag is immutable, I believe. You'd have to do something like this:
$(this).replaceWith($('<h5>' + this.innerHTML + '</h5>'));
Change the type of element using jQuery
You can use the replaceWith
method to do this. You'll need to rebuild the attributes for the replacement though.
$('#p').replaceWith(function(){
return '<span>' + $(this).contents().text() + '</span>';
});
Working example
jQuery convert DOM element to different type
Sans-jQuery solution:
function makeNewElementFromElement( tag, elem ) {
var newElem = document.createElement(tag),
i, prop,
attr = elem.attributes,
attrLen = attr.length;
// Copy children
elem = elem.cloneNode(true);
while (elem.firstChild) {
newElem.appendChild(elem.firstChild);
}
// Copy DOM properties
for (i in elem) {
try {
prop = elem[i];
if (prop && i !== 'outerHTML' && (typeof prop === 'string' || typeof prop === 'number')) {
newElem[i] = elem[i];
}
} catch(e) { /* some props throw getter errors */ }
}
// Copy attributes
for (i = 0; i < attrLen; i++) {
newElem.setAttribute(attr[i].nodeName, attr[i].nodeValue);
}
// Copy inline CSS
newElem.style.cssText = elem.style.cssText;
return newElem;
}
E.g.
makeNewElementFromElement('a', someDivElement); // Create anchor from div
Related Topics
Dynamically Add CSS to Page via JavaScript
Fix First Column of a Bootstrap Table
How to Get Color Value from Gradient by Percentage with JavaScript
Affix Is Not Working in Bootstrap 4 (Alpha)
Jquery/JavaScript Opacity Animation with Scroll
Clean Way to Programmatically Use CSS Transitions from Js
Center an Image Vertically and Horizontally Using CSS
Set Height of <Div> = to Height of Another <Div> Through .Css
How to Get a Floating Footer to Stick to the Bottom of the Viewport in Ie 6
IE9: Why Setting "-Ms-Transform" Works from CSS, But Not with Jquery.Css()
How to Combine Jquery Animate with CSS3 Properties Without Using CSS Transitions
Move the Mouse Pointer to a Specific Position
Best Way to Get All Selected Checkboxes Values in Jquery
Get Index of Element as Child Relative to Parent
How to Get Value at a Specific Index of Array in JavaScript