Using Javascript how to exchange the id of textbox [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can I exchange ids of two textboxes?
I have two textboxes,
<input type="text" id="name1" name="fname"/>
and
<input type="text" id="name2" name="lname"/>
programatically through Javascript, I want to exchange the ids name1 and name2 of both the textboxes. Please suggest the right way to achieve this.

function swapId( elem1, elem2 ) {
var temp = elem1.id;
elem1.id = elem2.id;
elem2.id = temp;
}
swapId( document.getElementById('name1'), document.getElementById('name2') );
Which is a very simplified example. There should be more checking if passed in values really are references to actual DOM nodes etc. But this will work just fine.

Exchanging the IDs is as simple as getting a reference to the two text boxes, then assigning the IDs you want them to have. While it is invalid to temporarily have two elements with the same ID, no browser will prevent you from doing so.
var fname = document.getElementById('name1');
var lname = document.getElementById('name2');
fname.setAttribute('id', 'name2');
lname.setAttribute('id', 'name1');

Try this code...
var id1 = document.getElementById('name1');
var id2 = document.getElementById('name2');
id1.setAttribute("id", "name2");
id2.setAttribute("id", "name1");

Related

Unable to get form data by tag name in jQuery [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to get form data in jQuery, I have tried two following methods:
var form = document.getElementById('user_form'); //By using form id
var form = document.getElementsByTagName('form'); //By form tag name
the first one gives me the result in the following format:
<form id="user_form" action="/users/create_user" method="post">
// ...
</form>
which is ok, but when I try the second method then it returns me the result in the form of object.
[form#user_form, user_form: form#user_form]
but I am looking for the result in the same format which 1st method returns.
getElementsByTagName returns a list of nodes which satisties that tag name.
You can apply indexing to get a single element.
var forms = document.getElementsByTagName('form'); //By form tag name
var form = forms[0];
You can easily use jquery selector to access the node you want with just indexing. Here is an example with vanilla and jquery:
var htmlFormById = $("#user_form")[0];
console.log("htmlFormById",htmlFormById);
var htmlFormByTag = $("form")[0];
console.log("htmlFormByTag",htmlFormByTag);
var formById = document.getElementById('user_form');
console.log(formById);
var formByTag = document.getElementsByTagName('form')[0];
console.log(formByTag);
Here is a working solution in the fiddle: https://jsfiddle.net/toc3ej6s/1/
var InnerHtml = document.getElementById("wrapper").innerHTML
InnerHtml = serialize(document.forms[0]); //Serialized Form Data
Found That on this one JavaScript Form serialized
Hope this Helps.
The getElementsByTagName() method returns a collection of all elements in the document with the specified tag name
document.getElementsByTagName('form')

How to switch <td> content with another <td> [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So the thing goes as following: I'm a begginer programmer (so far i know HTML, CSS some JavaScript and some C++) and we have a school project to create a chessboard with figures on them but I want to go a step further and be able to move the figures.
So far I've used the prompt function to get the coordinates and move them around but that feels far too much stone-agish. Now what I wish to accomplish is to be able to click on a , copy its content into a variable and upon clicking on another replace its content with the one stored in the variable. (I'll deal with the rules later..)
Each has its unique id and I have used element.firstChild.nodeValue to acquire the content.. Any suggestions on how to do this in JavaScript without using jQuery (if it can't be done in JavaScript then by all means do it in some other language.. it's about time I start learning them anyway.. :P)
If each <td> has a unique id, what about:
var lastStoredValue = "", lastClicked = ""; // = "" is important in that case
var t1 = document.getElementById("t1"); // td with id="t1"
var t2 = document.getElementById("t2"); // td with id="t2"
t1.onclick = function() {
if (lastClicked !== "t1" && lastStoredValue.trim())
t1.innerHTML = lastStoredValue; // replace its content with the one stored in the variable
if (!lastStoredValue.match(new RegExp(t1.innerHTML)))
lastStoredValue= t1.innerHTML; // copy its content into a variable
lastClicked = "t1";
}
t2.onclick = function() {
if (lastClicked !== "t2" && lastStoredValue.trim())
t2.innerHTML = lastStoredValue; // replace its content with the one stored in the variable
if (!lastStoredValue.match(new RegExp(t2.innerHTML)))
lastStoredValue= t2.innerHTML; // copy its content into a variable
lastClicked = "t2";
}
This gets what is stored into one td and puts it into a variable; then stores what is in the variable into another td when click on that td.

Facing error while getting value of dynamically created textbox [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have created a dynamic textbox but unable to fetch its value on runtime. it shows something else.
code
var UL = document.createElement('ul');
var Li = document.createElement('li');
var A4 = document.createElement('input');
A4.type = 'text';
A4.setAttribute('id', 'current_page');
A4.value = list.length;
A4.setAttribute('style', 'width:30px;height:26px;text-align:center;position:relative;left:35px');
Li.appendChild(A4);
UL.appendChild(Li);
script
var va = $('#current_page').val;
alert(va);
Seeing as you're using JavaScript for everything else, save the jQuery for another day:
var va = document.getElementById('current_page').value;
In jQuery you need to use .val(); instead of .val;
var va = $('#current_page').val();
alert(va);
It doesn't look like you're ever appending anything to the document.
You need to use $('#current_page').val(); to get the value
For future reference can check if your selectors are finding anything using:
console.log($('#current_page'));
Use .val() in jQuery and it is a function
var val = $('#current_page').val();
alert(val);
Using Native JavaScript, You can use document.getElementById
alert(document.getElementById('current_page').value)

How return this data in json for each? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
i have json values like this, i try to use for each to get the result one bye one, but it was not returning to me
"[{"id":6,"userid":13,"title":"business3","bussinessTypeid":1,"photurl":"","cntry":[{"id":"9","name":"Bahrain"}]},
{"id":7,"userid":13,"title":"business6 arabic","bussinessTypeid":1,"photurl":"","cntry":[{"id":"1","name":"Afghanistan"}]},
{"id":10,"userid":13,"title":"E-commerce","bussinessTypeid":1,"photurl":"","cntry":[{"id":"1","name":"Afghanistan"}]},
{"id":11,"userid":8,"title":"Auto phone parts","bussinessTypeid":1,"photurl":"","cntry":[{"id":"1","name":"Afghanistan"}]},
{"id":19,"userid":8,"title":"التجارة الإلكترونية","bussinessTypeid":1,"photurl":".jpg","cntry":[{"id":"9","name":"Bahrain"}]},
{"id":20,"userid":8,"title":"E-commerce -online shopping","bussinessTypeid":8,"photurl":".jpg","cntry":[{"id":"9","name":"Bahrain"}]},
{"id":21,"userid":13,"title":"My new Business","bussinessTypeid":6,"photurl":".jpg","cntry":[{"id":"9","name":"Bahrain"}]}]"
can anyone guide me to achive this
I think it's being treated as a string try to remove the ( " ) on the start and end of it something like:
var json = [{"id":6,"userid":13,"title":"business3","bussinessTypeid":1,"photurl":"","cntry":[{"id":"9","name":"Bahrain"}]},{"id":7,"userid":13,"title":"business6 arabic","bussinessTypeid":1,"photurl":"","cntry":[{"id":"1","name":"Afghanistan"}]},{"id":10,"userid":13,"title":"E-commerce","bussinessTypeid":1,"photurl":"","cntry":[{"id":"1","name":"Afghanistan"}]},{"id":11,"userid":8,"title":"Auto phone parts","bussinessTypeid":1,"photurl":"","cntry":[{"id":"1","name":"Afghanistan"}]},{"id":19,"userid":8,"title":"التجارة الإلكترونية","bussinessTypeid":1,"photurl":".jpg","cntry":[{"id":"9","name":"Bahrain"}]},{"id":20,"userid":8,"title":"E-commerce -online shopping","bussinessTypeid":8,"photurl":".jpg","cntry":[{"id":"9","name":"Bahrain"}]},{"id":21,"userid":13,"title":"My new Business","bussinessTypeid":6,"photurl":".jpg","cntry":[{"id":"9","name":"Bahrain"}]}];
Then do the iteration:
for(id in json){
alert(json[id]['id']);
}
and don't include any next line spaces are ok as long as they are separated by a comma (,)
Use parseJson()
Description: Takes a well-formed JSON string and returns the resulting
JavaScript object.
$.each(data, function(key, value){
console.log(value.id, value.title);
$.each(value.cntry, function(key2, value2){
console.log(value2.id, value2.name);
})
});
Try this with jquery
var data = jQuery.parseJSON(yourJsonInString); //or this parser below
var data = JSON.parse(yourJsonInString);

Assign a variable variable in Javascript loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a loop with the jquery .each function that is filtering through IDs with the suffix _prop_val I then get the prefix and use it for other operations as you can see in the code below. At the moment I am having to use an if statement to set the rlPrice. Is there a way to get it in the loop i.e. prefixPrice and then have it set as rlPrice afterwards
$('[id$="_prop_val"]').each(function(){
var prefix = this.id.slice(0,2);
if( $('#'+prefix+'_prop_val').val() != ""){
$('#'+prefix+'_prop_val').prop('disabled', false).trigger('chosen:updated');
$('#'+prefix+'_ct_row').show();
$('#'+prefix+'_deactivate_btn').show();
if(prefix == "rl"){
rlPrice = $('#rl option:selected').data("unit-price");
}
checkExtra(prefix);
}else{
$('#'+prefix+'_container').addClass('inactive');
$('#'+prefix+'_activate_btn').show();
}
});
You might find an object more useful here. Define it before the loop:
var price = {};
Then in your loop just assign the price to a property with the prefix as a key - no need to check for a condition:
price[prefix] = $('#rl option:selected').data("unit-price");
Then you can just access the price using the prefix:
var rlPrice = price['rl'];

Categories