Facing error while getting value of dynamically created 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 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)

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.

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'];

Regex like parameter function in Javascript [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 will do a function with parameters that get a regex and check the field of a form with it.
I have this code:
//The first function handler (no one runs):
field.onblur = function(){
checkField(0, /^([a-z ñáéíóúü]{2,60})$/i, "name", "nameError", "Error in name");
}
//The function:
function checkField(numForm, regex, idField, idError, error){
var form = document.getElementsByTagName("form")[numForm];
var field = form.getElementById(idField);
var spanError = form.getElementById(idError);
//Since here runs, so I think the problem is with the regex
if(!regex.test(idField.value))
spanError.innerHTML = error;
else
spanError.innerHTML = "";
}
What is the proper way to make a function and give it a regex like a parameter?
The regexp is passed fine, these two lines are erranous instead:
var field = form.getElementById(idField);
var spanError = form.getElementById(idError);
getElementById() is a method of document only. You can fix the issue by using id string like below:
var field = form[idField];
var spanError = form[idError];
... or using getElementById(): var field = document.getElementById(idField);
Also this line (unless a typo in the post)
if(!regex.test(idField.value))
should be:
if(!regex.test(field.value))

Using Javascript how to exchange the id of 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 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");

Categories