Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-22-2013, 08:41 AM   PM User | #1
njfail
Regular Coder

 
Join Date: Aug 2010
Posts: 125
Thanks: 2
Thanked 0 Times in 0 Posts
njfail is an unknown quantity at this point
How to delete and insert things using JS?

Hello everyone!

I'm using software on a site that only allows me to insert changes using a js file and a css file.
Sadly, I'm not familiar with js.

There is a snipit of code that I want to remove.
There are 4 tabs below, I want to remove the first 3.
How can I remove them using js?

Code:
<ul class="ui-tabs-nav">
	<li class="ui-state-default">
		<a href="#tabs-1">tab1</a>
	</li>
	<li class="ui-state-default">
		<a href="#tabs-2">tab2</a>
	</li>
	<li class="ui-state-default">
		<a href="#tabs-3">tab3</a>
	</li>
	<li class="ui-state-default">
		<a href="#tabs-4">tab4</a>
	</li>
</ul>
Out of curiousity, how would I add in things using JS?
Suppose I wanted to change the "tab4" text to something else?

Thank you all for the help over the past few years
njfail is offline   Reply With Quote
Old 02-22-2013, 09:23 AM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,615
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Seeing the “ui-…” class names I suppose you are using jQuery, right? I’m gonna move the thread to the JS frameworks section.

Now, would you mind to share with us why you want to remove and change the items with JS rather than by just editing the HTML? In which context is this being used?
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 02-22-2013, 09:38 AM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,468
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by njfail View Post
Out of curiousity, how would I add in things using JS?
Suppose I wanted to change the "tab4" text to something else?
To do that with ordinary JavaScript (without JQuery) you could use:

Code:
li = document.getElementsByClassName('ui-state-default');
len = li.length;
for (i = 0;i < len; i++) {
   if (li[i].firstChild.href == '#tabs-4') {
      li[i].firstChild.innerHTML = ';replacement text';
   }
}
Note that assumes that all the <li> tags have an <a> tag as the first tag inside them so if some of them don't you'd need to add extra code to find the <a> tags. Also if you want it to work on really old browsers you'd need to add code to define getElementsByClassName.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 02-22-2013, 04:43 PM   PM User | #4
njfail
Regular Coder

 
Join Date: Aug 2010
Posts: 125
Thanks: 2
Thanked 0 Times in 0 Posts
njfail is an unknown quantity at this point
Quote:
Originally Posted by VIPStephan View Post
Seeing the “ui-…” class names I suppose you are using jQuery, right? I’m gonna move the thread to the JS frameworks section.

Now, would you mind to share with us why you want to remove and change the items with JS rather than by just editing the HTML? In which context is this being used?

Hey Stephan,
I cannot edit the HTML, because I do not have access to edit the HTML file. I can only edit it by appending a .js file and a .css file.
njfail is offline   Reply With Quote
Old 02-22-2013, 06:30 PM   PM User | #5
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
one of the best ways to deal with things like lists is to iterate over them with the each method, which keeps an index that you can use to operate on elements according to their position:

Code:
$(".ui-tabs-nav li").each(function (idx) {
    if (idx != 3) {
        $(this).remove()
    } else {
        $(this).text("whatever")
    }
});
xelawho is offline   Reply With Quote
Old 02-22-2013, 06:34 PM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,468
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by njfail View Post
Hey Stephan,
I cannot edit the HTML, because I do not have access to edit the HTML file. I can only edit it by appending a .js file and a .css file.
No one has suggested editing the HTML. All the code supplied has been JavaScript.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 02-22-2013, 06:46 PM   PM User | #7
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
Quote:
Originally Posted by VIPStephan View Post
Now, would you mind to share with us why you want to remove and change the items with JS rather than by just editing the HTML?
xelawho is offline   Reply With Quote
Old 02-22-2013, 09:57 PM   PM User | #8
njfail
Regular Coder

 
Join Date: Aug 2010
Posts: 125
Thanks: 2
Thanked 0 Times in 0 Posts
njfail is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post
No one has suggested editing the HTML. All the code supplied has been JavaScript.
I was replying to VIPStephen. I quoted him asking me why I don't just edit the HTML, and answered his question.

I appreciate all the JS tips! I will be trying these out tonight
njfail is offline   Reply With Quote
Old 02-23-2013, 06:48 AM   PM User | #9
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
why not simply hide it with css?

Code:
.ui-tabs-nav .ui-state-default { display: none; }
.ui-tabs-nav .ui-state-default:first-child { display: list-item; }
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 02-25-2013, 10:22 PM   PM User | #10
njfail
Regular Coder

 
Join Date: Aug 2010
Posts: 125
Thanks: 2
Thanked 0 Times in 0 Posts
njfail is an unknown quantity at this point
Hello everyone, thanks for the help so far.
I decided instead of removing them, I will just rename them using the JS that felgall supplied (Thanks!).

I have another question;

For each tab, it has a description inside it. I want to change the words in the description, so delete it and then rewrite it.
Could someone tell me how I would do that? And could you try to explain each line of code so I can try to apply it to more situations

Code:
div id="tabs-1" class="row ui-tabs-panel ui-widget-content ui-corner-bottom">
<section class="instructions">
<h3>The fastest ad code in the world</h3>
njfail is offline   Reply With Quote
Old 02-27-2013, 05:16 AM   PM User | #11
njfail
Regular Coder

 
Join Date: Aug 2010
Posts: 125
Thanks: 2
Thanked 0 Times in 0 Posts
njfail is an unknown quantity at this point
Hey guys,
I played around with the JS and I got something that kind of works...
It does what I want it to do, but on some other pages, it messes things up. I've got no idea why its doing that. Everything after the favicon line is messing up other pages. I tried doing one set of replacing vars at a time and each one works, but also makes slight changes to other pages. There don't seem to be any shared ID's or classes though. So I think it might just be a syntax problem or something.. Is there a problem with the way I wrote this?

Code:
$(document).ready(function() {

$('#networkname h1').append('<img id="logo" src="http://valueviewmedia.com/valueviewmedia.png"/>');

$('#favicon').attr("href","http://valueviewmedia.com/favicon.ico");


var haystackText = document.getElementById("adcode_tabs").innerHTML;

var matchText = 'Standard';

var replacementText = 'Advanced';

var replaced = haystackText.replace(matchText, replacementText);

document.getElementById("adcode_tabs").innerHTML = replaced;


var haystackTexta = document.getElementById("adcode_tabs").innerHTML;

var matchTexta = 'Email';

var replacementTexta = 'HTML';

var replaceda = haystackTexta.replace(matchTexta, replacementTexta);

document.getElementById("adcode_tabs").innerHTML = replaceda;


var haystackTextb = document.getElementById("adcode_tabs").innerHTML;

var matchTextb = '3rd Party Ad Server';

var replacementTextb = 'JavaScript';

var replacedb = haystackTextb.replace(matchTextb, replacementTextb);

document.getElementById("adcode_tabs").innerHTML = replacedb;

var haystackTextc = document.getElementById("tabs-1").innerHTML;

var matchTextc = 'The fastest ad code in the world';

var replacementTextc = 'Advanced 2 part ad code';

var replacedc = haystackTextc.replace(matchTextc, replacementTextc);

document.getElementById("tabs-1").innerHTML = replacedc;


var haystackTextd = document.getElementById("tabs-1").innerHTML;

var matchTextd = 'Our standard ad code is 100% asychronous and loads all ad tags in a single call to the server. It is compatible with most third parties, including rich media providers. It won\'t work with some legacy rich media providers, if you need to support them use the Synchronous ad code.';

var replacementTextd = 'This code is for advanced users. We recommend normal publishers use the JavaScript code tab. This advanced 2 part code is 100% asynchronous and loads all ad tags with a single call to the server.';

var replacedd = haystackTextd.replace(matchTextd, replacementTextd);

document.getElementById("tabs-1").innerHTML = replacedd;


var haystackTexte = document.getElementById("tabs-3").innerHTML;

var matchTexte = 'Serve Ads in Email';

var replacementTexte = 'Basic HTML ad code';

var replacede = haystackTexte.replace(matchTexte, replacementTexte);

document.getElementById("tabs-3").innerHTML = replacede;


var haystackTextf = document.getElementById("tabs-3").innerHTML;

var matchTextf = 'Due to the nature of Email this ad code has limitations (including only being able to serve images and not flash or JavaScript/HTML.) It should only be used when Javascript code cannot be used.';

var replacementTextf = 'We do not recommend using the HTML code. It is best to use the JavaScript code tab. The HTML code tab should only be used for specially approved Email advertising.';

var replacedf = haystackTextf.replace(matchTextf, replacementTextf);

document.getElementById("tabs-3").innerHTML = replacedf;


var haystackTextg = document.getElementById("tabs-4").innerHTML;

var matchTextg = 'Serve ads through other ad servers';

var replacementTextg = 'Recommended - JavaScript ad code';

var replacedg = haystackTextg.replace(matchTextg, replacementTextg);

document.getElementById("tabs-4").innerHTML = replacedg;


var haystackTexth = document.getElementById("tabs-4").innerHTML;

var matchTexth = 'You can serve ads through other ad servers using this simple code. This code isn\'t 100% asychronous but can be served through a friendly iframe in another ad server to achieve 100% asychronous ad serving.';

var replacementTexth = 'This is our standard ad code. Place this code in your website\'s HTML file in the place you want the ad to display. This code is asynchronous, meaning it will delay your website\'s content from loading.';

var replacedh = haystackTexth.replace(matchTexth, replacementTexth);

document.getElementById("tabs-4").innerHTML = replacedh;

});
njfail is offline   Reply With Quote
Reply

Bookmarks

Tags
code, delete, insert, javascript, remove

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:19 AM.


Advertisement
Log in to turn off these ads.