How input and remove field with javascript? - javascript

i would like to create two buttons, one where the user can press it and then appears a drop drown and a input text field and another to remove this one, if the user wishes.
I already searched it in Google but can't find it.
Best regards,
Valter Henrique.

[working demo here: http://jsfiddle.net/GNnSw/1/][1]
your html
<div id="box" style="display:none;">
<select>
<option value="test">Test</option>
</select>
<input type="text" value="" id="text1" />
<input type="text" value="" id="text2" />
</div>
<input type="button" value="show" id="show" />
<input type="button" value="hide" id="hide" />
in jQuery:
$('#show').live('click', function(){
$('#box').show();
});
$('#hide').live('click', function(){
$('#box').hide();
});
http://jsfiddle.net/GNnSw/4/

using jquery it would take something like:
<button onclick="$('form').show();">press it</button>
<form>
//input elements
</form>
Search harder in google btw.

Do it with jQuery.
HTML
<button id="buttonid" value="Click on me!">
jQuery
$("#buttonid").click(function(){
var $input = '<input id="inputid" type="text" value="value">';
// make the input field
var $select = '<select id="selectid"></select>';
// make the select
var $opt1 = '<option name="one">one</option>';
var $opt2 = '<option name="two">two</option>';
// make two options
$select.append($opt1).append($opt2);
// append to select the options
$(this).after('<form action="url" method="POST"></form>').append($input).append($select);
// append input and the select after the button
});
Oh yeah. :) Btw you need jquery library.

Create a "placeholder" for your fields:
<div id="placeholder"></div>
Add the buttons / links:
<a onClick="add()">Add Form</a><a onClick="remove()">Remove Form</a>
And this to your javascript-file:
function add() {
document.getElementById('placeholder').innerHTML = "Code for your form...";
}
function remove() {
document.getElementById('placeholder').innerHTML = "";
}

I guess the best way of achieving flexible and user friendly HTML layout it by using external JavaScript library, such as jQuery or mootools. The reason is - in traditional web frameworks after you send HTML content to web browser, server cannot manipulate with it. Also, I guess good principle is to use Java only for serving content, and using client-side framework to do all the magic with User Interface.
Moreover, You will find plenty of examples how to work with those libraries like this one.
If you would really like to stick to plain Java, since you might know anything about JavaScript, I suggest checking out Google Web Toolkit and Vaadin. You can write Java code almost without any restrictions, and it will be "converted" (compiled) to JavaScript automatically. But that decision should be considered deeply, since learning GWT or Vaading might be more time consuming and not always applicable.

Related

Form TextArea Losing Linebreaks

I am trying to repair an existing web form that submits a text area's contents to an external site's shopping cart service. The textarea is named "adtext" and upon submission it runs a few different scripts to calculate pricing, etc. It ultimately re-writes the ad content into a value named op31 (which is recognized by the shopping cart). The cart system recently got updated and it broke our script to convert line breaks in this text area into something that would be retained in that other site. I've tried looking at other sites, but it's over my head. I'm not particularly good at this stuff. I'm sure this isn't, and likely wasn't the best way to do it. I've seen CSS suggestions but don't understand it enough to actually implement them.
I've stripped out as much code as I comfortably could to clean it up, but still retain the issue. I'm wondering if someone could assist me with updating this function into something that would convert the "adtext" textarea's line breaks into something usable when written to "op31".
<script language="JavaScript" type="text/JavaScript">
function ConvertCarriageReturns(textarea, strReplace){
document.form.op31.value = escape(textarea.value)
for(i=0;i<document.form.op31.value.length;i++){
if(document.form.op31.value.indexOf("%0D%0A") > -1 ){
document.form.op31.value = document.form.op31.value.replace("%0D%0A",strReplace)
}
}
document.form.op31.value = unescape(document.form.op31.value)}
</script>
<form
action="https://(cart's url)/addtocart.aspx"
method="post"
name="form">
<textarea name="adtext" rows="12"></textarea>
<input alt="Add To Cart" name="add"
onclick="ConvertCarriageReturns(this.form.adtext,'<br>');
return checkwords(this)" src="https://....Add-To-Cart.gif"
type="image" />
<input name="item" type="hidden" value="(misc cart parameters" />
<input name="op31" readonly="readonly" type="hidden" />
</form>
You can use this native PHP function called nl2br
Like this:
$text = nl2br(this.form.adtext);

Variable List Form Input Jquery and PHP

So I am creating a form that has a feature(s) input. It starts off with only 1 but the number may increase. By clicking on a button I will append a new text box for new feature to be written into it via Jquery.
JsFiddle: https://jsfiddle.net/kmckeow/ewnetzqx/
Issue 1:error from java script saying that I cannot use input.attr().
Issue 2:When I do get this JavaScript to work, how am I supposed to reference a variable number of elements i.e. (feature1, feature2, feature3, ...) in PHP. When I submit a form with post I usually access the information in each element via it's name. How can I do that when I am not sure how many features there will be.
HTML:
<div id="new_product">
<form action="" method="post" enctype="multipart/form-data">
<p>
<label for="feature1"> Feature(s): </label>
<input type="text" name="feature1" />
<button type="button" class="btn btn-primary" id="add_feature">Add Feature</button>
<p>
<input type="submit" value="Submit" name="submit">
</form>
</div>
JQuery:
var feature_count=1;
var feature_count_old=0;
$( "#add_feature" ).click(function() {
feature_count_old=feature_count;
feature_count+=1;
var input = document.createElement("input");
input.attr('name', 'feature' + feature_count);
input.attr('type', 'text');
$("input[name='feature'+feature_count_old]").after(input);
});
Issue 1
You did not load jQuery, so $ is not defined. Simply add the jQuery library to your snippet and you're good to go!
Issue 2
attr() is a jQuery method and not vanilla JS, so you need to do something like this:
var $input = $('input');
$input.attr('name', 'feature' + feature_count);

Change Input field grabbing NAME

I've searched about all I can. I'm trying to change the text of an input field using its name. I have found many ways to do it by ID like:
<script>
function changeValue(o){
document.getElementById('type').value=o.innerHTML;
}
</script>
<button id="technician" onclick="changeValue(this)">Technician</button>
<button id="developer" onclick="changeValue(this)">Developer</button>
<input type="text" id="type" name="type" value="change" />
But what I need to accomplish is for inputs without ID's.
Something along the lines of:
<script>
function changeValue(o){
document.getElementsByName('NAME').value=o.innerHTML;
}
</script>
<button id="technician" onclick="changeValue(this)">Technician</button>
<button id="developer" onclick="changeValue(this)">Developer</button>
<input type="text" name="NAME" value="change" />
Is there any way of accomplishing this?
UPDATE
I'm trying to expand on the javascript you guys helped me with.
The Snippet:
<script>
function changeValue(o){
document.getElementsByName('NAME')[0].value=o.innerHTML;
}
</script>
<span onclick="changeValue(this)" style="cursor: pointer;">One</span>
<span onclick="changeValue(this)" style="cursor: pointer;">Two</span>
<img src='image.gif' onclick="changeValue(this.src)" />
<input type="text" name="NAME" value="SOMETHING">
The spans are working correctly, although I don't actually need them. I will have all images once I figure this out.
I have tried a few ways, but what I can find is not directly related to my use.
The end goal is to get the img src into the text input with js, preferably somewhat how it already exists. I feel it's really close.
getElementsByName() returns a collection. use [] to access individual elements
ex :
function changeValue(o){
document.getElementsByName('NAME')[0].value=o.innerHTML;
}
document.getElementsByName('NAME') returns a list of elements by name. You need to provide the index as
document.getElementsByName('NAME')[0].value=o.innerHTML
Use document.querySelector like so
document.querySelector('input[name="NAME"]').value = o.innerHTML;
jQuery way
$('input[name="NAME"]').val("im changed!")

Javascript get values of multiple text fields

I have a dynamic web page where the content may contain between 1 and 10 links, provided in text boxes, similar to the following:
<input size="50" id="link" value="http://Something.Something" type="text">
<input size="50" id="link" value="http://SomethingElse.Something" type="text">
I need javascript to be able to read all of the links, and be able to manipulate the data (store in array, output to screen, etc)
I know that I can read a single id using the following
var link = document.getElementById('link');
Which will return the first match - but, how can I do a loop or obtain all the values for all the links, bearing in mind that the number of links cannot be determined beforehand?
P.S. I have tried using getElementsByTagName('input') but there are more inputs on the page, which means it's getting more results than I'd like it to get.
You can make them all have names and search by name.
<input name="vrow" value="0" type="text"/>
<input name="vrow" value="0" type="text"/>
<input name="vrow" value="0" type="text"/>
<input name="vrow" value="0" type="text"/>
Then you can get it with:
var vrows = document.getElementsByName("vrow");
alert(vrows.length);
Give them all a common class and access using document.getElementsByClassName('class').
IDs should be unique for each element. You could use document.getElementsByClassName or document.querySelectorAll(".class"); and then use the class name (assuming relatively modern browser). Or use document.getElementsByTagName() and then iterate through the elements comparing with the class.
Attach a jQuery lib and you will be able to do something like:
$('input[type=text]').each(function(i, val){
alert($(this).val());
});

Changing form information with Javascript?

Is there a way to change the content of a form with Javascript?
I have a series of paypal buttons, but want the user to be able to shop in CAD or USD.
The form looks something like this:
<form onsubmit="return false;">
<input name="amount" value="6.99" type="hidden" />
<input name="currency_code" value="CAD" type="hidden" />
</form>
I need to change the value="CAD' to value="USD"
Is there any way I could have a button or some sort of 'toggle' on the site whereby users can change the currency? It should happen to each product.
I had someone implement simple cart, but they are no longer available
is there a better way to do this?( I though this might be simplest) But perhaps it should be done during checkout somehow? I presume this would be more complicated.
thanks in advance and hope I have been clear! let me know if you need more information.
my site is www.bodesi.com (if that helps)
You can do it via javascript DOM.
Example: <input type="button" onclick="document.getElementsByName('currency_code')[0].value='USD';">
You can do this easily with jQuery, which it looks like your site already includes.
Here's one way to do it: http://jsfiddle.net/x7LDN/
$(function() {
$('#change_currency').click(function(){
$('input[name="currency_code"]').val('USD');
});
});
If I have understood you requirement properly ,
Ask user somewhere for the currency
<select name = "currency" id='currency' >
<option value='USD'>USD</option>
<option value='CAD'>CAD</option>
</select>
And later use the javascript on the same page to change the value of the paypal button
If you are using jquery:
(function($){
$("#currency").change(function(){
$('input[name="currency_code"]').val($(this).val());
});
})(jQuery);
If you want to use clean javascript then
document.getElementById('currency').onchange = function(){
document.getElementsByName('currency_code')[0].value= this.value;
}

Categories