Replacing inner div content? - javascript

I'd like to replace the following inner div content:
<div class="price"><span class="unit">$</span>37</div>
with this:
<del>$37</del> $19
The $37 gets striked through and the new price is displayed next to it. The above <div> is generated dynamically.
I have tried the following but it isn't doing anything:
$( "div.price" ).replaceWith("<del>$37</del> $19");
I'm trying this in jquery but is there a way to do it in angular? I guess the issue with angular is that I can't add directives to the above code.

run the code on document ready
// Code goes here
$(function(){
$( "div.price" ).html("<del>$37</del> $19")
});

Related

jQuery .insertAfter not working, trying to move element in DOM

I have tried the following jQuery code to move a <select> element (that I can only see within the DOM) after a <form> element but it does not work:
<script type="text/javascript">
jQuery('[id^=lc_currency]').insertAfter('[id^=lc_change]');
</script>
I have also tried:
<script type="text/javascript">
jQuery('select[id^=lc_currency]').insertAfter('form[id^=lc_change]');
</script>
Any coding suggestions? I am using a wildcard selector because on every page the ID changes, 1c_currency1, 1c_currency2, etc.
Live site is at http://thehungrygeek.com/2015/11/17/australia-dairy-company/
I want to move specifically a select dropdown box, from one place to another on a webpage. Unfortunately the element code is only located in the DOM.
This select dropdown box is located at the bottom of the page at http://thehungrygeek.com/2015/11/17/australia-dairy-company/
The code, only located in the DOM, is as follows:
<select style="width:200px" name="lc_currency1" id="lc_currency1" onchange="localCurrencyChange('SGD',lcValues1,1)">...</select>
The select dropdown box is supposed to be moved here:
The relevant code at the area is as follows:
<form name="lc_change1" id="lc_change1" action="http://thehungrygeek.com/2015/11/17/australia-dairy-company/" method="post">
Show currencies in
<noscript>[Please enable JavaScript to change the currency used on this page]</noscript>
<br>
<small>Powered by LocalCurrency. Rates from Yahoo! Finance</small>
</form>
Thanks for any help!
1st: Be sure you include jquery
2nd: Wrap your code in
$(document).ready(function(){
// your code here
});
3rd: Try to use .each()
$(document).ready(function(){
jQuery('select[id^=lc_currency]').each(function(){
var getformId = $(this).attr('id').replace('currency','change');
//alert(getformId);
$(this).insertAfter('form#'+ getformId);
});
});
Working Demo
I guess your id strings should be inside quote , Try this
$(function(){
jQuery("select[id^='lc_currency']").insertAfter("form[id^='lc_change']");
});
Edit:
Try to move the select element after 1 sec after window load. this might works for you as your select element coming from javascript code.
$(window).load(function(){
setTimeout(function(){
jQuery("form[id^='lc_change']").prepend( "select[id^='lc_currency']");
},1000);
});
From the screenshots you added, it seems like you want to insert the select inside the form, not after, this is what I used:
jQuery('[id^=lc_currency]').insertAfter('[id^=lc_change] noscript');

Php simple html dom remove div with specific class

I am using the code bellow to get some content. My problem is that i don't know how to avoid/bypass/remove a specific div.
html Structure:
<div class="post-single-content box mark-links">
<div class="sharebar-wrap">dfdfdfd</div>
</div>
And i an using the code bellow to get the content:
foreach($html->find('div[class=post-single-content box mark-links]') as $table)
{
$arr44[]= $table->innertext ;
}
How can avoid or remove or bypass to grab the div with class sharebar-wrap?
I don't need it!
Cheers!!
You can indeed use jQuery as Héctor E mentioned, otherwise you can use raw javascript :
document.querySelector('.sharebar-wrap').remove();
With javascript:
var aux = document.getElementByClass('sharebar-wrap');
aux.parentNode.removeChild(aux);
With jQuery:
$( ".sharebar-wrap" ).remove()

Change text of child

I'm trying to make a add to favorite system. I have a function which alerts the proper id I want to add.
I use:
<script type="text/javascript">
function addfavo(state_name)
{
alert(state_name);
}
</script>
And in my html I have a loop (with php) which shows all the images with the add to favorite links which looks like.
<div style="margin-top:40px;">
<a onclick="addfavo('<?php echo $imgid ?>')"><b>Add to favourits</b></a>
</div>
So what happens is I have a lot of links to the same function with different parameters, but I only want the link that I click to change the text (to something like added to favorites)
Can some one help me in the right direction?
I have tried adding:
$(this).innerHTML("test");
but it didn't work.
You might want to use the html method:
$(this).html('test');
While html is a jQuery method, innerHTML is a property of a DOM element. If you were using pure JavaScript, you'd probably use:
this.innerHTML = 'test';
However, as you are using the onclick attribute on your HTML tag, this will not point to your current DOM element inside your function scope. In your case, I'd add a class to your elements, like add_favorite and add your text to another attribute:
<div style="margin-top:40px;">
<b>Add to favourits</b>
</div>
And then apply a jQuery event to it:
<script type="text/javascript">
$(function() {
$('.add-favorite').click(function(e) {
var text = $(this).data('text'); // store the text in a variable
$(this).html(text); // replace your element's html with your text
e.preventDefault();
});
});
</script>
Fiddle: http://jsfiddle.net/MH6vY/

How to get child class of parent id using jquery?

I just want to ask, How can I get the class of child element.
For example if I have this kind of code:
<a class="w010 h010 p007 b03 d2 f1" id="category_entertainment">
<div class="it3 ib3 il3 jt05 jb05 jl10 kt04 kb04 kl03">
//some codes here. . .
</div>
</a>
Now I have an anchor tag. My scenario is after clicking the tag. I need to get the class of the div and replace it with my new one.
$("#category_entertainment").on('click',function(){
var new_class = "it3 ir3 il3 jt10 jr05 jl05 kt03 kr04 kl04";
$("#category_entertainment > div).attr("class",new_class); //i tried this but not working.
});
This worked correctly on my machine:
$("#category_entertainment").on('click',function(){
var new_class = "it3 ir3 il3 jt10 jr05 jl05 kt03 kr04 kl04";
$("#category_entertainment div").attr("class",new_class);
});
My only suggestion would be to make sure this code abode is running after the elements have been created. If you place this in window.onload=function() {// assign click handler} something like that to assure that the element is loaded. Below is a jsfiddle example.
jsfiddle example

Grails rendered content accessing id elements dynamically

AJAX content is being rendered with a remoteLink function inside the form to populate a accordion (just a little background info).
The function attatchEmail(test) which is being called on the double-click of each paragraph content of the JQuery Accordion widget. This is what happens running the function... Screenshot of 1st alert & Screenshot of 2nd alert.
Is it not possible to select the paragraph and get the contents from the paragraph like below?
(I have tried changing .val to .html and .text. I have also tried $('#'+testingID))
_form.GSP
function attatchEmail(test) {
$(document).ready(function()
{
var testingID = test.id;
alert(testingID);
var testingValue = $(testingID).val();
alert(testingValue);
});
};
_contactListAjax.GSP
<g:each in="${contactList}" status = "i" var="contact">
<h3>${contact.contactSurname +', '+ contact.contactForename}</h3>
<div><p id="contact${contact.id}" ondblclick="attatchEmail(this)">${'Email: '+contact.email}</p></div>
</g:each>
Run out of avenues to explore, I'm sure I've done something simple like this before perfectly fine :/
See two screenshots please for better insight, thanks
Well, it looks like you're using jQuery but it also looks like you're not really buying into the jQuery methodology. Mixing behavior with markup is not really considered a good practice these days, especially when using a library like jQuery. I'm not sure of your exact issue but I would recommend changing it to something like the following:
<g:each in="${contactList}" status = "i" var="contact">
<h3>${contact.contactSurname +', '+ contact.contactForename}</h3>
<div><p class="contact-email" data-id="${contact.id}">${'Email: '+contact.email}</p></div>
</g:each>
$(function() {
$("body").on("dblclick", ".contact-email", attachEmail);
});
function attatchEmail(event) {
var $element = $(event.target);
var id = $element.data("id");
};
This should help fix any issues with dynamic rendering because of the way the jQuery on function works.

Categories