I've tried to access buttons in my menu. I only want to add listeners to the items that is in the XML file im loading.
The thing is, i dont know how to call a button i've named "Var1_btn" when i've got a string "Var1".
Does anyone know how to call buttons from a for-loop?
for each(var chapter in presentation_xml.*)
{
chapter + "_btn".addEventListener(MouseEvent.MOUSE_DOWN, traceit);
}
is what i came up with...
Assuming you load the xml into a variable called presentationXML, it's like this:
for each(var chapter in presentationXML.*)
{
this[chapter + "_btn"].addEventListener(MouseEvent.MOUSE_DOWN, traceit);
}
You can use:
for each(var chapter in presentation_xml.*)
{
this[chapter + "_btn"].addEventListener(MouseEvent.MOUSE_DOWN, traceit);
}
but you could also use getChildByName, like this:
for each(var chapter in presentation_xml.*)
{
var myBtn:MovieClip = getChildByName(chapter + "_btn");
myBtn.addEventListener(MouseEvent.MOUSE_DOWN, traceit);
}
Here is a good post on when to use getChildByName.
DisplayObjectContainer::getChildByName()
Better use chapter.toString().
The same effect, but other coder will read it ad understand, that chapter is being converted from XML to its string representation when concatenates with string literal.
Related
I am iterating through a LARGE list of objects all of which will open the same modal window that will be loaded with dynamic information. To make this work, I create a counter called MenuCounter that I know increments just fine.
That said, I am attempting to wrap a hyperlink around the icons I need to use and the injection of the method keeps pointing to the last value of the MenuCounter.
I first tried this:
...
When I ran into the issue, I tried reducing the code to the following but then the page somehow activates the hyperlink and the modal window appears and will not go away.
...
Can somebody please help me out?
Thank you!
You should apply a lambda expression to the Blazor #onclick directive instead of using the onclick Html attribute, in which case it should call a JS function, which you did not mean.
Note that I've introduced a new directive to prevent the default action of the anchor element: #onclick:preventDefault
Test this code:
#page "/"
<a href="#" #onclick:preventDefault #onclick="#(() => SetupChangeName(MenuCounter))" >Click me...</a>
<div>Counter is #output</div>
#code
{
private int MenuCounter = 10;
private int output;
private void SetupChangeName (int counter)
{
output = counter;
}
}
Note: If you use a for loop to render a list of anchor elements, you must define a variable local to the loop, and provide it as the input to your lambda expression, something like this:
#for(int MenuCounter = 0; MenuCounter < 10; MenuCounter++)
{
int local= MenuCounter;
<a href="#" #onclick:preventDefault #onclick="#(() =>
SetupChangeName(local))" >Click me...</a>
}
otherwise, all the lambda expressions will have the the same value for MenuCounter, which is the value incremented for the last iteration. See For loop not returning expected value - C# - Blazor explaining the issue.
I'm not a fan of onclick attributes, but if you're set on this method, I believe you just need to santize the C# and JS in the same line like this:
...
Adding the quotes will ensure at least an empty string is present for JS, and then you can process it.
Alternative method
Since mixing languages like that is quite frustrating, I find it easier to use data tags, for example
...
And then in your JS file:
var links = document.querySelectorAll('[data-menu-counter]');
links.forEach(x => x.addEventListener('click', /* your function code here */);
I would like to know whether is it possible to insert an input tag in Json file? I would like to add a checkbox in here
{
"data1":"here"
}
{"row"[{
"DATA1" :"<input type='checkbox'/>"
},
{
"DATA2" :"<input type='text'/>"
}
]}
Like this?
No. The closest you could get would probably be to represent a checkbox as a string.
{
"data1":"<input type=\"checkbox\"/>"
}
Then you would be able to insert that string as html into the DOM somewhere (assuming this is for an HTML webpage)
document.getElementById("someid").innerHTML = myJSONObject.data1;
Depending on what you are trying to do you might be able to use a function that returns a checkbox.
{
"data1":function(){
var box = document.createElement("input");
box.setAttribute("type", "checkbox");
return box;
}
}
Then in your code that parses the JSON you would need to call the data1 property as a function
var myCheckboxFromJSONFile = myJSONObject.data1();
I had found the solution.
Hopefully it can help any newbies outside
{
"data1":"<input type='checkbox'>"
}
Don't use double quotes(") use single quotes(')
This is my working program. I want to type my words in a text file instead of a dart [] List.
import 'dart:html';
List <String> words = ['testing','hurry','stop','test','work','lol'];
//How can I use a text file filled with words instead of this??
void main() {
querySelector("#reset").onClick.listen(randomWord);
}
void randomWord(MouseEvent e) {
words.shuffle();
querySelector("#random_word").text = words.last.toString();
}
It seems its not as easy as just doing List words =('listofwords.txt'); ? :( help
Here is a example of what I am trying to do
http://watchout4snakes.com/wo4snakes/Random/RandomWord
Since you're importing dart:html, I presume you're trying to do this from a Web app. In that case, you need an HttpRequest to load a file. If your text file has a list of words separated by spaces, you can do something like this:
import "dart:html";
List<String> words;
void main() {
HttpRequest.getString("listofwords.txt").then((String text) {
words = text.split(' ');
});
}
Note that loading files in this manner is asynchronous, so make sure you don't try to manipulate words before it's been filled. You'll want to trigger such manipulation within the then() callback block.
You would wanting to be using Dart IO. I would check out the API Docs and learn a thing or two about it, as it is very useful.
https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:io
Hope this helps!
Luca
Hi I need to parse XML file using jquery. I created read and display functionality. But when a word having single quote not working.
My XML is like this
<container>
<data name="Google" definition="A search engine"/>
<data name=" Mozilla's " definition="A web browser"/>
</ container>
using my jquery code I can read definition of Google. But I can't read Mozilla's definition due to that single quotes. This is my jquery code.
var displayDefinition = function(obj){
$.get("definitions.xml", function(data){
xml_data1.find("data[name^='"+obj.innerHTML+"']").each(function(k, v){
right=''+ $(this).attr("Defination") + '';
}
}
$(".result").append(right);
}
Any body knows the solution for this please help me.
Thanks
jQuery deals with single quotes very well. the structure of your function looks really wild though. I changed it a big assuming you want to create a function that can display the definition based on passing it a name: http://jsfiddle.net/rkw79/VQxZ2/
function display(id) {
$('container').find('data[name="' +id.trim()+ '"]').each(function() {
var right = $(this).attr("definition");
$(".result").html(right);
});
}
Note, you have to make sure your 'name' attribute does not begin or end with spaces; and just trim the string that the user passes in.
I'm trying to create a code snippet to remove all style attributes regardless of tag using HtmlAgilityPack.
Here's my code:
var elements = htmlDoc.DocumentNode.SelectNodes("//*");
if (elements!=null)
{
foreach (var element in elements)
{
element.Attributes.Remove("style");
}
}
However, I'm not getting it to stick? If I look at the element object immediately after Remove("style"). I can see that the style attribute has been removed, but it still appears in the DocumentNode object. :/
I'm feeling a bit stupid, but it seems off to me? Anyone done this using HtmlAgilityPack? Thanks!
Update
I changed my code to the following, and it works properly:
public static void RemoveStyleAttributes(this HtmlDocument html)
{
var elementsWithStyleAttribute = html.DocumentNode.SelectNodes("//#style");
if (elementsWithStyleAttribute!=null)
{
foreach (var element in elementsWithStyleAttribute)
{
element.Attributes["style"].Remove();
}
}
}
Your code snippet seems to be correct - it removes the attributes. The thing is, DocumentNode .InnerHtml(I assume you monitored this property) is a complex property, maybe it get updated after some unknown circumstances and you actually shouldn't use this property to get the document as a string. Instead of it HtmlDocument.Save method for this:
string result = null;
using (StringWriter writer = new StringWriter())
{
htmlDoc.Save(writer);
result = writer.ToString();
}
now result variable holds the string representation of your document.
One more thing: your code may be improved by changing your expression to "//*[#style]" which gets you only elements with style attribute.
Here is a very simple solution
VB.net
element.Attributes.Remove(element.Attributes("style"))
c#
element.Attributes.Remove(element.Attributes["style"])