I got this code:
#using (Html.BeginForm())
{
<h3 id="js" class="text-blue title-article editable">#Model.Title</h3>
<input type="text" id="testinput" name="testinput" />
<input type="submit" value="submit" onclick="changeLink()">
}
<script>
function changeLink()
{
var str =document.getElementById('js').innerHTML;
alert(str);
}
</script>
When I click the submit-button, the function gets the .innerhtml from the id="js".
I would like the var str to get copied into the textbox with the id="testinput".
You can get value from js element and equal to the testinput
<script>
function changeLink()
{
document.getElementById('js').innerHTML = document.getElementById('testinput').value
}
</script>
Try this..
<script>
function changeLink()
{
document.getElementById('testinput').value=str ;
}
</script>
Related
i have 2 pages: A.htm and B.htm
A calls B as a popup, then after some operations B must call a javascript inside A, to do this i used the javascript: "opener".
but i think i did some mistakes because in the popup when i click the button nothing happen, here an example to download: http:\www.suale.it/prova/a.zip
this is the code of A.htm
<html>
<script language="javascript">
function ricaricapagina()
{
var theform;
if
(
window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms["frmMain"];
}
else
{
theform = document.frmMain;
}
//txtOperation.value=pOperation;
theform.submit();
}
function SelectArticoli(pForm, txtOperation)
{
var sQueryString=new String;
var WinSettings = "center=yes,resizable=yes,scrollbars=yes,height=600,width=1000";
sQueryString='B.htm';
sQueryString += '?CallerForm=' + pForm;
sQueryString += '&CallerOperation=' + txtOperation;
sQueryString += '&CallerReload=1' ;
window.open(sQueryString, '', WinSettings);
}
</script>
<form name="frmMain" id="frmMain" action="A.htm" method="post">
<input name="txtOperation" type="hidden" id="txtOperation" />
<input name="cmdSelectArticoli" id="cmdSelectArticoli" onclick="javascript:SelectArticoli('frmMain', 'txtOperation');" type="button" value="Aggiungi Articoli...">
</form>
</html>
this is the code of B.htm
<html>
<script language="javascript">
function chiudipopup(pResult)
{
if (pResult==true)
{
window.opener.document.forms[frmMain].item(frmMain.txtCallerOperation.value).value = 'SelectArticoli';
if (frmMain.txtCallerReload.value != '')
{
window.opener.ricaricapagina();
}
}
window.close();
}
</script>
</script>
<form name="frmMain" method="post" id="frmMain">
<input name="txtCallerOperation" type="hidden" id="txtCallerOperation" value="txtOperation" />
<input type="submit" name="cmdConferma" onclick="javascript:chiudipopup(true);" value="conferma" id="cmdConferma" class="button" />
</form>
</html>
can you try declaring the function as window.ricaricapagina = function(){...}
You have two problem:
[B.html]=>change window.opener.document.forms[frmMain].item(frmMain.txtCallerOperation.value).value to window.opener.document.forms["frmMain"].elements["txtOperation"].value
[B.html]=>window.opener.ricaricapagina();(I'm working on it :))
I have an iFrame to create a message. However, when I press enter while typing in the iFrame, it creates another div, however, I want it to create a <br/>. I tried using the execCommand insertBrOnReturn, but I did not get it to work. Any idea's? Thanks in advance!
The JSFiddle in stackoverflow doesn't really work, but I have an existing one at "Craz1k0ek/d869w67b/" after the default jsfiddle.net
function iFrameOn() {
richTextField.document.designMode = 'On';
richTextField.document.body.style.fontFamily = 'Open Sans,Helvetica Neue,Helvetica,Arial,sans-serif';
}
function iBold() {
richTextField.document.execCommand('bold', false, null);
}
function iUnderline() {
richTextField.document.execCommand('underline', false, null);
}
function iItalic() {
richTextField.document.execCommand('italic', false, null);
}
function submit_form() {
var theForm = document.getElementById("myForm");
theForm.elements["myTextArea"].value = window.frames['richTextField'].document.body.innerHTML;
theForm.submit();
}
<body onload="iFrameOn()">
<form action="" name="myForm" id="myForm" method="post">
<p>
Title
<input name="title" id="title" type="text" size="80" maxlength="80">
</p>
<div id="wysiwyg_cp" style="padding:8px; width:700px;">
<input type="button" onClick="iBold()" value="B">
<input type="button" onClick="iUnderline()" value="U">
<input type="button" onClick="iItalic()" value="I">
</div>
<textarea style="display:none;" name="myTextArea" id="myTextArea" cols="100" rows="14"></textarea>
<iframe name="richTextField" id="richTextField" style="border:#000 1px solid; width:700px; height:300px;"></iframe>
<input name="myBtn" type="button" value="Submit Data" onClick="javascript:submit_form();">
</form>
</body>
I changed the submit_form function as follows:
function submit_form() {
remove_divs();
var theForm = document.getElementById("myForm");
theForm.elements["myTextArea"].value = window.frames['richTextField'].document.body.innerHTML;
theForm.submit();
}
And then I added the remove_divs function which is as follows:
function remove_divs() {
var frameHTML = window.frames['richTextField'].document.body.innerHTML;
frameHTML = frameHTML
.replace(/<div><br><\/div>/g, '')
.replace(/<div>/g, '<p>')
.replace(/<\/div>/g, '</p>');
$(window.frames['richTextField'].document.body).html(frameHTML);
}
It replaces all the <div><br></div> elements with nothing and then it replaces all the <div> elements with <p> and all the </div> elements with </p>. This formats the text in the field exactly as I want.
Then I had an issue with setting the DesignMode = 'On'; I solved that by placing extra code in the javascript file as follows:
window.onload = function() {
window.frames['richTextField'].document.designMode = "On";
}
I also got some minor warnings on how I gained access to the richTextField, so I replaced all the richTextField.document.execCommand elements for window.frames['richTextField'].document.execCommand.
I hope this helps!
I am trying to update a textbox based on whether or not a checkbox is checked or not. Thanks to this post I got a text box working fine, but I can't get a checkbox to update the value. What am I missing?
<html>
<head>
<title>sum totals</title>
<script type="text/javascript">
function calculate(t){
var j = document.getElementById("output");
var rege = /^[0-9]*$/;
if ( rege.test(t.tons.value) ) {
var treesSaved = t.tons.value * 17;
j.value = treesSaved;
}
else
alert("Error in input");
}
$('input[name="selectedItems1"]').click(function(){
var j = document.getElementById("output");
if (this.checked) {
j.value=j.value+300
}else{
j.value=j.value-300
}
});
</script>
</head>
<body>
<form>
<input type="text" placeholder="Tons" id="tons" onkeyup="calculate(this.form)"/>
<br />
<input type="checkbox" name="selectedItems1" value="val1" />I have a car
<br/>
<input type="text" id="output" value="Output" />
</form>
</body>
</html>
Place the <script> tag after <form>
Reason:
When the html page loads, it'll be interpreted line by line. When it come to click(), jQuery will try to find the element input[name="selectedItems1"] which won't be loaded into the DOM at that time. So, jQuery won't attach the click() event handle to that checkbox. That's the reason why your code didn't work.
Try this :
<html>
<head>
<title>sum totals</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script><!-- load jquery -->
<script type="text/javascript">
function calculate(){
var j = document.getElementById("output");
var rege = /^[0-9]*$/;
var tons = $('#tons').val();
if ( rege.test(tons) ) {
val = parseInt(tons);
var treesSaved = val * 17;
if($('input[name="selectedItems1"]').is(":checked"))
{
treesSaved = treesSaved +300;
}
else
{
treesSaved = treesSaved -300;
}
if(isNaN(treesSaved))
j.value=0
else
j.value=treesSaved;
}
else
alert("Error in input");
}
$(function(){
$('input[name="selectedItems1"]').change(function(){
calculate();
});
});
</script>
</head>
<body>
<form>
<input type="text" placeholder="Tons" id="tons" onkeyup="calculate()"/>
<br />
<input type="checkbox" name="selectedItems1" value="val1" />I have a car
<br/>
<input type="text" id="output" value="Output" />
</form>
</body>
</html>
The code should receive a sentence / string and print it in reverse, if word or letter that wrote in the filter contained within the word belongs to a string - the word will not print.
The question why my check alert "hi" not working?? tnx!
<html>
<head>
<script>
function myfunc() {
alert ("hi");
var count=0;
var phrase= document.getElementById('phrase').value;
var filter = document.getElementById('filter').value;
var arrReverse = phrase.split(" ").reverse();
for (i=0; i<arrReverse.length; i++) {
if (arrReverse[i].search(filter)==-1) {
if (i%2==0) {
document.getElementById('words').innerHTML="<span class="word"><u>"arrReverse[i]"</u><span>";
} else {
document.getElementById('words').innerHTML="<span class="word">"arrReverse[i]"<span>";
}
} else if (arrReverse[i].search(filter)!=-1) { count++; }
if (count>0) {
document.getElementById('count').innerHTML="<span class="count">"count "word(s) filtered out <span>";
}
}
</script>
</head>
<body >
<h1>Sentence Reverser!</h1>
<div> Phrase: <input id="phrase" type="text" size="40"/></div>
<div> Filter: <input id="filter" type="text" size="10"/></div>
<div><button id="go" onclick="myfunc()"> Go! </button></div>
<div id="words"></div>
<div id="count"></div>
</body>
</html>
Be careful with "" quotes and '' quotes:
.innerHTML="<span class="word"><u>"arrReverse[i]"</u><span>"; // wrong
.innerHTML="<span class='word'><u>"+arrReverse[i]+"</u><span>"; //right
For further reference check:
http://www.w3schools.com/js/js_obj_string.asp
Found a nice website for an alternative to using /forms in share point. My code below though does not print my input to the screen. Any Suggestions.
<script type="text/javascript">
function printvalues()
{
var div = document.getElementById("divId");
div.innerHTML += "test";
div.innerHTML += inputobj1.value;
}
</script>
<div id="divId">
username: <input name="inputobj1" value="" />
<button id="idButton" type="button" onclick ="printvalues();">
Generate!
</button>
<div id="divId">
</div>
There's two issues with your code.
First, you're missing a closing </div> tag; and you have two <div> elements with the same id property! I think you should remove the first div's id property.
Second, your <input /> is missing the id property, therefore it's not automatically defined by inputobj1.
Try this markup:
<script type="text/javascript">
function printvalues() {
var div = document.getElementById("divId");
div.innerHTML += "test";
div.innerHTML += document.getElementById("inputobj1").value;
}
</script>
<div>
username: <input id="inputobj1" name="inputobj1" value="" />
<button id="idButton" type="button" onclick ="printvalues();">Generate!</button>
<div id="divId"></div>
</div>
Here's a working jsFiddle.
<script type="text/javascript">
function printvalues() {
var div = document.getElementById("divId");
div.innerHTML += "test";
div.innerHTML += inputobj1.value;
}
</script>
<div id="divId2">
username: <input name="inputobj1" value="" />
<button id="idButton" type="button" onclick ="javascript:printvalues();">
Generate!
</button>
<div id="divId"></div>
</div>
or
<div id="divId2">
username: <input name="inputobj1" value="" />
<button id="idButton" type="button">
Generate!
</button>
<div id="divId"></div>
</div>
<script type="text/javascript">
function printvalues() {
var div = document.getElementById("divId");
div.innerHTML += "test";
div.innerHTML += inputobj1.value;
}
$('#idButton').click(function(){
printvalues()
})
</script>
You had 2 divId's as well, i made an assumption on witch on to use
this doesnst work because you have two div with id="idButton" and your markup is not closed
this may also work as you tagged jquery
function printvalues() {
var value = $('input[name="inputobj1"]').val();
var $div = $('<div>',{text:value});
$('#divId').append($div);
}
or using the divId div
function printvalues() {
var value = $('input[name="inputobj1"]').val();
$('#divId').text(value);
}