Calling a function from another function with arguments - javascript

I am a beginner to javascript ! I am trying to call a function from another function inside the same script tags. When I do this without sending arguments it works fine but when i do that with arguments that another function is not called..
Here is what I am doing
function measurement_convert()
{
var mc = "Measurement Conversion";
txt = "<h2>" + mc + "</h2> <br><br>";
txt +=
"<form action='' method='post'>" +
"Select Type <select name='conversion_type' onchange='loadXML('abcd')'> "+
"<option value='area'>Area</option>"+
"<option value='length'>Length</option>"+
"<option value='volume'>Volume</option>"+
"<option value='weight'>Weight</option>"+
"</select> <br><br>"
document.getElementById("content_main_top").innerHTML=txt;
}
So, I am calling the function from onchange of this form.
And here's the another function
function loadXML(var1)
{
document.getElementById("content_main_top").innerHTML=null;
document.getElementById("content_main_bottom").innerHTML=null;
}
Can anyone help me ?

Your problem is that you are trying to nest the same type of quote:
onchange='loadXML('abcd')'
This actually gets interpreted as:
onchange='loadXML('
Instead, try (note the escaped double-quotes, since this code is inside a PHP string):
Select Type <select name=\"conversion_type\" onchange=\"loadXML('abcd')\">

Your single quotes are clashing in the onchange value, as you use the same for closing the value in.
You can change it to:
"Select Type <select name='conversion_type' onchange=\"loadXML('abcd')\"> "

Related

Call a javascript function from anchor tag inside td inside javascript code

Only Basic javascript knowledge is required.If anyone knows please help me.
I don't know how to do it but i have tried it in many ways but still not able to pass the value.
Simply, i have a for loop and i have a td inside it. I want to call a javascript function from this td click but problem is i am unable to pass any parameter to this function.
the code is here :
function GetContractList(abc) {
var data = abc
for (var it in data) {
tab += "<tr>";
tab += "<td>" + data[it].ContractCode + "</td>";
if (data[it].ContractCode != "") {
var Contract = data[it].ContractCode;
tab += "<td><a onclick='Delete_User(Contract);'>View</td>";
// tab += "<td><a data=" + data[it].ContractCode + " href='javascript:Delete_User(this.data);'>View</td>";
}
else {
tab += "<td></td>";
}
As u can see, i have tried to pass parameter to Delete_User function but the syntax is somewhere broken.
tab += "<td><a onclick='Delete_User(Contract);'>View</td>";
this line gives error-- Contract is not defined.
"<td><a data=" + data[it].ContractCode + " href='javascript:Delete_User(this.data);'>View</td>".
This line also doesnot passes any value to the function.
Please someone help me out.
try:
var cont = data[it].ContractCode.replace(/"/g, '\\"');
tab += '<td><a onclick="Delete_User(\''+cont+'\');">View</td>";
first line encodes any double quotes
the second line inserts the contract as a parameter and adds single quotes around it
You need to escape the quote for the parameter of the function.
tab += '<td>View</td>';
// ^^ ^^
function Delete_User(id) {
console.log('Delete_User', id);
}
var cc = '4711abc',
tab = 'View';
document.write(tab);

What could cause ViewData["x"].ToString to not work?

I want to display a colorpicker on an MVC5 application. This feature works if manually typed out and adapted for each page that uses it. Obviously, this is excessively duplicated code so I created a Utils method to generate the data. Here is the definition for that method:
public static string GetColorsList()
{
string colorsList = "<option style=\"color:gray\" value=\"null\">select color</option>" +
"<option style=\"color:azure\" value=\"Azure\">Azure</option>" +
"<option style=\"color:blue\" value=\"Blue\">Blue</option>" +
"<option style=\"color:cyan\" value=\"Cyan\">Cyan</option>" +
"<option style=\"color:green\" value=\"Green\">Green</option>" +
"<option style=\"color:magenta\" value=\"Magenta\">Magenta</option>" +
"<option style=\"color:orange\" value=\"Orange\">Orange</option>" +
"<option style=\"color:red\" value=\"Red\">Red</option>" +
"<option style=\"color:violet\" value=\"Violet\">Violet</option>" +
"<option style=\"color:yellow\" value=\"Yellow\">Yellow</option>";
return colorsList;
}
The reason I return a string instead of an IEnumerable is that a JS method adds new data (i.e. create a new row in the table) to the page using:
$(#tablename tbody).append(newcontent);
where new content is a string of the HTML to insert. Ideally I could just create an Html.DropDownList to display the picker contents but this does not return a string-friendly result.
What I want to accomplish: take colorsList, put in in ViewData["x"], turn ViewData["x"] into a string, and concat that string with other content that belongs in newcontent.
ViewData["x"].ToString() yields the following result:
<option style="color:gray" value="null">select
color</option><option style="color:azure" value="
Azure">Azure</option><option style=
"color:blue"
value="Blue">Blue</option><option style="
color:cyan" value="Cyan">Cyan</option><
option style="color:green" value="Green">
Green</option><option style="color:magenta" value=
"Magenta">Magenta</option><option style=
"color:orange" value="Orange">Orange</option
><option style="color:red" value="Red">
Red</option><option style="color:violet" value=
"Violet">Violet</option><option style=
"color:yellow" value="Yellow">Yellow</option>);
The workaround here is to run:
colorpickerContent = colorpickerContent.replace("$lt;", "<");
colorpickerContent = colorpickerContent.replace("$gt;", ">");
colorpickerContent = colorpickerContent.replace(""", "\"");
So that all the proper characters exist. When running, the program throws a syntax error because the output does not start with the quotation marks needed to consider it a string. Any suggestions on how I can make ViewData["x"] a workable string? I attempted JSON.stringify() but found similar results.
You're looking for the HtmlString that one won't be escaped.

Function not called via javascript in href

I used a code that I found in the web and then changed a little thing:
<script type="text/javascript">
var foodList = [];
function addToFood (addFood) {
alert(addFood);
//foodList.push(addFood);
//for (i = 0; i < foodList.length; i++) {
// var newFood = "<a href='#' onClick='removeRecord(" + i + ");'>X</a> " + foodList[i] + " <br>";
//};
//document.getElementById('foods').innerHTML += newFood;
}
</script>
At this moment I only want to alert the parameter on addToFood. The function should be called:
echo "<a href='javascript:addToFood(". $zeile1['TITLE'] .");' class='band-head'>Add</a>";
this line shows how I add an <a> with the javascript in the href. The php render the right line but the addToFood is never called.
Have a look at the online Demo: http://anthraxx.sytes.net/ maybe it can help you more then me.
The error I get via Google Chrome: Uncaught SyntaxError: Unexpected identifier But just can't figure that unexpected identifier out.
Thanks
You need to wrap the $zeile1['TITLE'] inside a string, as such:
echo "<a href='javascript:addToFood(\"". $zeile1['TITLE'] ."\");' class='band-head'>Add</a>";
You can see that I added escaped quotes \" after the opening parenthesis, and before the ending one, in the JS-call.
Otherwise it will try to pass an variable to the function, instead of a string.
Why escaped? That is because you are echo:ing it with PHP. If I didn't escape the quote, the PHP would interpret it as I was ending the string, which is not what we want. I want to put the quotes in there as inline-code.
Just scope your vaiable between ' ' or " " ,When you don't scope String's ,javascript read it like Var.
You need to escape the $zeile1['TITLE'], thats true and you can do that by using following line -
echo "<a href='javascript:addToFood(\"". $zeile1['TITLE'] ."\");' class='band-head'>Add</a>";
This is working.
try this
echo 'Add';
Replace :
echo "<a href='javascript:addToFood(". $zeile1['TITLE'] .");' class='band-head'>Add</a>";
With:
echo "<a href='javascript:addToFood(\"". $zeile1['TITLE'] ."\");' class='band-head'>Add</a>";

onchange event handler failing to call function

I have a function that uses:
name = "textBox" + (h) + (j);
id = "textBox" + (h) + (j);
value = hoursEachDay[j-1];
textBox = "<input type='text' maxlength='6' size='6' name='" + name + "'id='"+ id +"' value='" + value + "' onchange='updateHrs()'>";//or parent.updateHrs(j)
(h, i,and j being integers) to call another function that follows directly after the first function. That function is :
function updateHrs()// or updateHrs(dayOfMonth)
{
alert("This is the day changed ");
//or alert("This is the day changed " + dayOfMonth);
return;
}
I get a 'function not defined' error from firebug when I run this and trigger the onchange event handler. I have, on the suggestion of a coworker, changed updateHrs() to parent.updateHrs(), which actually works for some reason, until I try to pass a variable into the updateHrs() function (ass seen commented out above), at which point it declares that updateHrs(j) is not defined.
I am guessing that somehow the updateHrs function is being read as out of scope, although I'm not really sure how. Both of these functions are right after one another and both are right before the tag (yes there is a tag well above them as well), so they should not have a scope issue, not that I'm aware of anyway.
Thanks for any help that you can provide.

Call a function from a html added with html() function

I have this function into a javascript file :
function toggle_concessions(concessions) {
var text =
"<table>"+
"<tr><td class='concession-name'>gfhgfbfghfd</td><td class='op-encours'>| 15 opérations en cours</td></tr>"+
"<tr class='stats'><td class='concession-adresse'>ghfhdfhdgh</td><td class='voir-concessions'><img id='11' src='img/voirlesoperations.jpg' onclick='toggle_operations('ffff');'></td></tr>"+
"</table>";
;
if($("#"+concessions).attr("class")!="concessions toggled"){
$("#"+concessions).html(text);
$("#"+concessions).toggleClass("toggled");
}else{
$("#"+concessions).toggleClass("");
}
$("#"+concessions).toggle("slow");
}
The function "toggle_operations()" isn't working when I click on the image. Even when I execute an alert it doesn't work.
What can I do ?
Thanks
your html-embedded js is not well formed due to wrong pairing of string delimiters - you have to escape the quotes surrounding the argument to you toggle_operations call:
"<tr class='stats'><td class='concession-adresse'>ghfhdfhdgh</td><td class='voir-concessions'><img id='11' src='img/voirlesoperations.jpg' onclick='toggle_operations(\'ffff\');'></td></tr>"+

Categories