The links work in this example, but the onClick does nothing. When I display the productURL[i] in an alert() it shows the correct URL. Any suggestions?
var output='<table class="api-table">';
output+='<thead><tr><th colspan="2">' + productSubstrateName + '</th></tr></thead>';
for (var i=0;i<productURL.length;i++) {
output+='<tr>';
output+='<td style=\"cursor:pointer;\" onClick=\"'+productURL[i]+'\">'+productSubstrateAmounts[i]+'</td>';
output+='<td style=\"cursor:pointer;\" onClick=\"'+productURL[i]+'\">'+productSubstratePrices[i]+'</td>';
output+='</tr>';
}
output+="</table>";
$('#'+outputdiv).append(output);
but the onClick does nothing.
it does nothing becuase you have done nothing there ..you are just printing the value ..
onClick=\"'+productURL[i]+'\"
//--^^^^^^^^^^^----
if you need to do something then you can call a function there
onClick="myFunction("'+productURL[i]+'")"
and your function
function myFunction(obj){
alert(obj);
}
and you don't have to use \ there
Beginners mistake, thanks bipen, I forgot the document.location.href. :-(
for (var i=0;i<productURL.length;i++) {
output+='<tr>';
output+='<td style="cursor:pointer;" onClick="document.location.href=\''+productURL[i]+'\';">'+productSubstrateAmounts[i]+'</td>';
output+='<td style="cursor:pointer;" onClick="document.location.href=\''+productURL[i]+'\';">'+productSubstratePrices[i]+'</td>';
output+='</tr>';
}
Related
I am getting syntax error
Uncaught SyntaxError: missing ) after argument list
My code
<button onclick="alert('document.getElementById('todo.age').value');">onclick</button>
in controller add
$scope.alert = function(age) {
alert(age);
}
in html change like this:
<button ng-click="alert(todo.age);">onclick</button>
Try this:
<button onclick="alert(document.getElementById('todo.age').value);">onclick</button>
You shouldn't quote the argument to alert if you want to call the function.
It will start working better if you separate your HTML code and JavaScript.
Kind of this.
HTML:
<button id="button">onclick</button>
JavaScript:
window.onload = function()
{
var button = document.getElementById('button');
button.onclick = function()
{
//do some action here
}
}
This will help you to avoid simple syntax errors.
if u need value of the todo.age id values use the following code. <button onclick="alert(document.getElementById('todo.age').value);">onclick</button>
or else of u want to print the whole text document.getElementById('todo.age').value like this
use
<button onclick="alert('document.getElementById('todo.age').value');">onclick</button> code/
i am trying to alert id of a button (which is generated using jquery ) but when i alert its value it not coming right. heres the code
function removeimg(name,btn12){
//make request to remove the name
// $("#" + name).closest('li').remove();
// $("#" + btn12).remove();
// document.getElementById(name).style.display = 'none';
// document.getElementById(btn12).style.display = 'none';
var str = "#" + btn12;
alert(str);
alert($(str).val());
}
here is the link
http://shri-ram.lifekloud.com/pilot/step4.php
when you uplaod a image under the tab "add delete photo" the button is generated
i am trying to alert id of a button
val() does not get the id of an element; val returns the value element.
To get the id of an element, use attr
alert($(str).attr('id'));
Just a stab in the dark from your comment well its not even returning value thts the issue. but the id name is getting displayed correctly
If you have
<input type='button' id='b' value='btn' />
then
alert($('#b').val());
will in fact display btn. That said, if you have
<button id='b'>btn</button>
then nothing will be displayed. But like I said that's just a stab in the dark. It's impossible to know better without the html available (and I'm afraid I don't have time to parse through your site)
You have one meta-character . in your id #btnheader-8878374.png, That is the problem.
Just escape like this
$('.#btnheader-8878374\\.png')
and try you will get your concept working.
Full code,
var str = "#" + btn12;
str = str.replace('.','\\\\');
alert($(str).val());
Your problem is most likely that you do not have the value attribute set on your buttons, thus calling val() returns nothing.
If you want the text of the button just call text().
See jsFiddle
HTML
<button id="btn12">Button 12</button>
JQUERY
var str = "#" + "btn12";
alert( str ); // yields #btn12
alert( $(str).val() ); // yields nothing
alert( $(str).text() ); // yields Button 12
I have a problem with jQuery("#someID").html. It only prints the last key from the JSON.
Here is the js:
<div class="row" id="fetchmember">
<script type="text/javascript">
jQuery('#group').change(function() {
var id_group = this.value;
var memberjson = "fetchmember.php?group="+id_group;
jQuery.getJSON(memberjson, function(data) {
jQuery.each(data, function(i, items) {
jQuery("#fetchmember").html("<li>"+items.name+"</li>");
});
});
});
</script>
</div>
JSON result from one of the selected option :
[{"id":"1645819602","name":"Michael English","first_name":"Michael","last_name":"English"},
{"id":"100000251643877","name":"Bill Gaither","first_name":"Bill","last_name":"Gaither"}]
I want to print all of name from the json, but it print only last name key of the json. What's wrong with my code?
Any advice and helps would be greatly appreciated. Thank you very much
You're erasing the content on each iteration. Use append instead of html
Instead of
jQuery("#fetchmember").html("<li>"+items.name+"</li>");
Use
jQuery("#fetchmember").append("<li>"+items.name+"</li>");
At iteration, you overwrite the content with last one.
Better to use .append instead of .html, but you have to make the area empty before : jQuery("#fetchmember").empty();
I have the following label control:
<asp:Label ForeColor="DarkGreen" runat="server" ID="lblStatus"></asp:Label>
Its value is filled in the Page_Load event.
I attached the following Javascript (placed at the end of the page, not Master page):
function Validate() {
var lblObj = document.getElementById('<%=lblStatus.ClientID%>');
alert(lblObj.value);
if (lblObj.value == "Replaced" || lblObj.value == 'Trashed' || lblObj.value == "Internal Use") {
alert("Products with" + lblObj.value + "status cannot be reserved");
return false;
}
}
The alert(lblObj.value) displays a popup with text "undefined". How can I fix this problem? Please, I tried many combinations for placing the JavaScript but no luck! Thanks
UPDATE
Browser soruce code:
<span id="ctl00__main_lblStatus" style="color:DarkGreen;">Available</span></td>
First line of Validate JS function:
function Validate() {
var lblObj = document.getElementById('ctl00__main_lblStatus');
labels don't have a value. They have innerHTML and innerText.
Use JQuery and it will run in all browsers and platforms, something like:
$('#<%= lblStatus.ClientID %>').next().text();
source: JQuery: getting the value/text/innerHtml of a checkbox in an ASP.NET CheckBoxList control
Label server control renders as span. So you should get it's content by innerText. try this :
alert(lblObj.innerText);
ASP.NET label server control will be rendered in complex HTML output.
Like:
<span id="ctl00_ctl00_ContentPlaceHolder1_BodyPlaceHolder_lblLanguage0">
<label class="inputText">English</label>
</span>
When you use getElementById you will get span.
But to set value via javascript you have to access inner label object
try this
<script language="javascript" type="text/javascript">
function getlabelvalue()
{
var value1 = document.getElementById('<%=labelID.ClientID%>').value;
if (value1.length < 1)
value1 = 0;
}
</script>
With jquery you need to use the html method.
var g = $('#<%=lblStatus.ClientID%>').html();
These will NOT work with jquery:
$('#<%=lblStatus.ClientID%>').innerText
$('#<%=lblStatus.ClientID%>').innerHTML
$('#<%=lblStatus.ClientID%>').val()
hiere is my code, within a loop.
I want to solve my closure problem.
But on click I receive an error message "Syntac Error".
Any help. Thanks in advance.
var html = '<div style="margin-top:10px">';
html += '<div class="weiter"> Kartenauswahl in die Recherche übernehmen </div>';
html += '<div class="weiter" style="display:block;clear:left;margin-top:7px"
onclick="function(obj) { return getChildObject(obj)}(obj)">Weiter</div>';
html += '</div>';
This certainly does not look right:
function(obj) { return getChildObject(obj)}(obj)
I would simply write this:
return getChildObject(obj);
This code in your onclick attribute is not valid:
function(obj) { return getChildObject(obj)}(obj)
Put the function expression in parentheses and try this instead:
(function(obj) { return getChildObject(obj); })(obj)
If that's your actual code, the immediate problem is that there's a line break in your string literal, between the third and fourth lines. Delete that line break. There are problems in the HTML too, as mentioned in other answers.
The problem seems to be at:
onclick="function(obj) { return getChildObject(obj)}(obj)
^
Try removing (obj).
Also you should post the code for getChildObject function.
function(obj) { return getChildObject(obj)}(obj)
Above does not seems to be correct