Simple DOM cloneNode issue - dom

im having another rather simple issue with using cloneNode. I'm trying to duplicate a li node back into its own ul using java and I can't seem to find why its not working.
I have tried multiple things but I cant for the life of me figure it out.
I want this:
<ul id="mylist">
<li><h3>My Image:<img src="myImage.png"></h3></li>
</ul>
To look like this:
<ul id="mylist">
<li><h3>My Image:<img src="myImage.png"></h3></li>
<li><h3>My Image:<img src="myImage.png"></h3></li>
</ul>
I am using this function:
function myFunction(){
parent=document.getElementById("myList");
child=parent.getElementbyTagName("li")[0];
clone= child.cloneNode(true);
parent.insertBefore(clone,child);
}
</script>
Unsure as to why its not working, I pulled the code almost identically from an online source. If someone could help with my newbie question I would very much appreciate it.

I often find when copying javascript from the inter-rent, the author has never checked his code for typing errors or maybe they're deliberate to encourage a bit of understanding.
This works:
<ul id="myawesomelist">
<li id="x"><h3>My awesome image:<img src="myawesomeimage.png"></h3></li>
</ul>
and here's the javascript:
function myawesomefunction()
{
var myawesomeparent=document.getElementById('myawesomelist');
var myawesomechild=myawesomeparent.getElementsByTagName('li')[0];
var myawesomeclone=myawesomechild.cloneNode(true);
myawesomeparent.insertBefore(myawesomeclone,myawesomechild);
}
And here's the fiddle I built it in:
http://jsfiddle.net/g51n8sor/

Related

Beautify makes JSX ugly please look at the below scenario. I have tried a solution from a previous question with no success

The beautify extension in VSCode gives the below output. please find the code below.
<div className="container">
<span className="text-muted">
An amazing place to buy medicines.
</span>
</div>
When i attempt to beautify the above code i get the below output.
< div className = "container" >
<
span className = "text-muted" >
An amazing place to buy medicines. <
/span> <
/div>
Please suggest a solution? We are looking to implement standard coding practices in our project seems to have hit a roadblock here.
Have you tried this? : https://stackoverflow.com/a/61740593/260229
"files.associations": {
"*.jsx": "javascriptreact"
}

child div blocks with class names using emmet

I want to have nested div blocks with class names using Emmet codes but I cannot figure it out.
Something like this:
<div class="map">
<div class="cardcontainer">
</div>
</div>
I know div>div will produce a nested div block and I can do .map+.cardcontainer to produce two sibling divs, but .map>.cardcontainer does not work.
thanks.
div.map>div.cardcontainer
works for me. And the intellisense in vscode as you type that is great.

New to jquery. Starts with selector breaks the code. what's is wrong?

Thanks for the help. I'm trying to use a jQuery selector to watch for a click on a group of elements, that start with particular characters. I have come up with the following code, but I must be missing something. If I hard code the ID (ie. $("#test_1")...), the code works:
<body>
<div id="content">
<div id="parentcontainer">
<div id="test_1"></div>
</div>
</div>
</body>
<script>
$(window).load(function(){
$("#statusbar").text("Ready");
$("#parentcontainer").click(function(){alert("parent clicked");});
$("#btnaddelement").click(function(){alert("Add Button Clicked");});
$("[name^='test_']").click(function(e){e.stopPropagation();
alert("Child Clicked");});
});
</script>
You are selecting on $("[name^='test_']") which will give you elements who have a name attribute that start with test_. You need to select on $("[id^='test_']") for elements with an id that start with test_. That is one example of what you are getting with your hard-coded success of $('#test_1') -- an element whose id attribute is test_1.
Also, be aware if you are not already that xpath is the language used for selectors, so you can do all kinds of incredible selection if you become familiar with it.
Yes, you missed something. Change the div's attribute id to name will work
<div name="test_1"></div>
Actually, class was used more frequently.
And there are an opinion I want to improve the code.
Try to use the jquery's $(document).ready instead of DOM's load. Because load will wait for all the sources to be loaded compeletely before the js code can be executed, for example, all the photos are downloading ok.
I hope this help!

jquery .html() returning null in ie7

On click li element i am getting the current element value and appending it into another div dynamically.Its working fine in all browsers.But returning null in IE7.I don`t the reason that why its happening?Please can any one give me a solution for this..Part of the code only i pasted here.
Sample code:
////////////.//This line returning null in IE7./////////////////
$('#pagelink_a #pagelinkli_'+tab_lastid_val).html()
(tab_lastid_val value can be a 1 or 2 or 3.Clixked li element value comes here)
<div class="pagelink">
<div id="pagelink_a">
<ul>
/******** all li element are clickable***********/
<li id="pagelinkli_1"><a>Google</a></li>
<li id="pagelinkli_2"><a>Chrome</a></li>
<li id="pagelinkli_3"><a>Firefox</a></li>
</ul>
</div>
</div?
try this:
$('#pagelink_a').find('li[id=pagelinkli_'+tab_lastid_val']').html();
code is not tested but i think it should work.
Given your html layout, your parent div is .pagelink not #pagelink_a , so replace your following line:
$('#pagelink_a #pagelinkli_'+tab_lastid_val).html()
for this one:
$('.pagelink #pagelinkli_'+tab_lastid_val).html()
Just use
$('#pagelinkli_'+tab_lastid_val).html()
The # tag identifies an ID which only a single element may have. There is no need to have anything preceding it. You also labeled the previous class as id, which is wrong. I don't know how your other browsers managed to get anything.
Although bit off topic, it may be better to actually drop IE7 support entirely. Due to small user base and decreasing popularity, it may be costing you more money by support it than to not support it.
Try instead of html() , and try append().
For example
$('#ID').append('Your content');

MVC3 and Razor - How to place a dynamic value for hidden field?

I'm a beginner about Razor, and sometimes I get stuck with really simple things.
I have this foreach loop:
#foreach (dynamic item in ViewBag.EAList)
{
<li>
#using (#Html.BeginForm("Duplicate, "Daily"))
{
<p>#item.AuthorComment</p>
#Html.Hidden("EstadoDeAlmaID", #item.EAID)
#Html.Hidden("PosterID", Session["id"].ToString())
<input type="submit" value="Send" />
}
</li>
}
This line:
#Html.Hidden("EstadoDeAlmaID", #item.EAID)
Doesn't work, and I don't know how to make it work, I tried many ways, without #, with (--), with #(--)...
Could someone help me to display the dynamic value in my hidden field?
In addition, if someone know about a good Razor samples websites, I would be very thankful.
I had the same problem, found that a simple cast solved my problem.
#Html.Hidden("id", (string) ViewBag.ebook.isbn)
In Razor, once you are in "C# land", you no longer need to prefix values with # sign.
This should suffice:
#Html.Hidden("EstadoDeAlmaID", item.EAID)
Check out Scott Gu's article covering the syntax for more help.
Update
And I would also move your <li></li> within your using block, as Razor works better when you wrap HTML blocks inside of a code blocks.
Also, your Html.BeginForm should live outside of your loop.
#using (#Html.BeginForm("Duplicate, "Daily"))
{
<ul>
#foreach (? item in ViewBag.EAList)
{
<li>
<p>#item.AuthorComment</p>
#Html.Hidden("EstadoDeAlmaID", item.EAID)
#Html.Hidden("PosterID", Session["id"].ToString())
<input type="submit" value="Send" />
</li>
}
</ul>
}
Where ? in the foreach loop is the type of your items in EAList.
To avoid the Extension methods cannot be dynamically dispatched exception, use a model instead of ViewBag so you will not be using dynamic objects (this will avoid all the unnecessary casting in the View and is more in line with MVC style in general):
In your action when you return the view:
return View("ViewName", db.EAList.ToList());
In your view, the first line should be:
#model IEnumerable<EAListItem> //or whatever the type name is
Then just do:
#foreach(var item in Model)
You got the error, "Extension methods cannot be dynamically dispatched"... therein lies your trouble.
You should declare you loop variable not to be of type dynamic, but of the actual type in the collection. Then remove the # from the item.EAID call inside the #Html.Hidden() call.
The simple solution for me was to use ViewData instead of ViewBag. ViewBag is just a dynamic wrapper around ViewData anyway.
#Html.Hidden("ReportID", ViewData["ReportID"])
but I don't know if this will help in your case or not since you are creating dynamic items in your foreach loop.
I have found that when i want to use the view bag data in the HTML
Getting back to basics has often worked for me
<input type="hidden" name="Data" id="Data" value="#ViewBag.Data" />
this gave the same result.