Can I rename a field of my form using Javascript? Like changing:
<input type="text" name="chicken" id="chicken"/>
to
<input type="text" name="horse" id="horse"/>
Can I rename a field of my form using Javascript? Like changing:
<input type="text" name="chicken" id="chicken"/>
to
<input type="text" name="horse" id="horse"/>
Of course it is possible:
var field = document.getElementById("chicken");
field.id = "horse"; // using element properties
field.setAttribute("name", "horse"); // using .setAttribute() method
.id renames the object. apparently it changes the id.You can change the name attribute, but in my opinion, you shouldn't change the id.
You can use :
document.getElementById("chicken").setAttribute('name', 'horse');
Edit
The following statement is purely subjective, and might not have some room on SO.
From W3.org :
The id attribute has several roles in HTML:
- As a style sheet selector.
- As a target anchor for hypertext links.
- As a means to reference a particular element from a script.
- As the name of a declared OBJECT element.
- For general purpose processing by user agents (e.g. for identifying fields when extracting data from HTML pages into a database,
translating HTML documents into other formats, etc.)
As I see it, the ID is an unique identifier which is really useful to retrieve an element from a script (among other things), and I personally want to be able to retrieve this single element with the same selector whatever manipulation it endures during runtime. So, I won't change it.
Example : if I transform a chicken into a horse, I keep in mind that it used to be a chicken before, and I'd rather remember it as a mutant which I'll select with $('#chicken[name=horse]'), and fake it as an horse using others CSS selectors.
how to rename a tab in JavaScript. Below is the code.
var i=parseInt(1);
var clickid;
$(document).ready(function(){
$("#demoTabs").tabs();
$(".nav-tabs").on("click", "a", function(e){
e.preventDefault();
// $(this).tabs("show");
})
.on("click", ".close", function () {
var w_demo=$('#demoTabs').outerWidth();
$(this).parent().remove();
$("#demoTabs").width((w_demo-105)+"px");
});
$('.add-tab').click(function(e) {
e.preventDefault();
var html="<li class=\"tabs\" id="+i+"><a href=\"#\">New Tab</a><span class=\"close\">x</span></li>";
$(this).closest('li').before(html);
var w=$("#demoTabs").outerWidth();
var w=w+95;
document.getElementById('demoTabs').style.width=w+"px";
i=i+1;
});
if (document.getElementById('demoTabs').addEventListener) {
document.getElementById('demoTabs').addEventListener('contextmenu', function(e) {
$("#rmenu").toggleClass("hide");
$("#rmenu").css(
{
position: "absolute",
top: e.pageY,
left: e.pageX
}
);
e.preventDefault();
clickid=$(this).id;
prompt(this+" "+clickid);
}, false);
}
// this is from another SO post...
$(document).bind("click", function(event) {
document.getElementById("rmenu").className = "hide";
});
$("#refresh").click(function(){
location.reload();
})
$("#rename").click(function(e){
var reply=prompt("type in a name for tab");
if(reply!=null && reply.trim().length>0)
{
// $(e.target).prev("tabs").text(reply);
// prompt(clickid);
document.getElementByClass("").innerHTML=reply;
}
});
});
#body{
height:22.8px;
width:32.4px;
}
#demoTabs{
width:90px;
overflow: auto;
}
.nav-tabs > li > span {
float:right;
cursor:pointer;
position:absolute;
right: 6px;
top: 8px;
color: red;
}
.hide {
display: none;
}
#rmenu {
border: 1px solid black;
background-color: white;
cursor:pointer;
}
#rmenu ul {
padding: 0;
list-style: none;
margin: 0;
}
#rmenu li{
list-style: none;
padding-left: 5px;
padding-right: 5px;
}
<body style="position:relative">
<div id="demoTabs">
<ul class="nav nav-tabs">
<li class="tabs"><a href="#" class="add-tab">+Add</a></li>
</ul></div>
<div class="tab-content">
</div>
<div class="hide" id="rmenu">
<ul>
<li id="refresh">Refresh</li>
<li id="rename">Rename</li>
</ul>
</div>