JavaScript - Remove First Occurrence Of String.
Is it possible to convert "xyz123456xyz789" into "123456xyz789"? What I'm trying to say is, remove the first occurence of "xyz" from the string, so that all the other occurrences of "xyz" that aren't the first one, remain. Help is appreciated.
Thanks, Jazzo Similar TutorialsHello, I'm trying to remove one of the $ signs from this string below. I've gotten as far as removing them both. I'm having trouble removing one of them. Can someone help? Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> <script> $(document).ready(function(){ /*v = $("span.name").html().replace(/,/g,''); $("span.name").html(v);*/ v = $("div.test td").html().replace(/\$/g,''); $("div.test td").html(v); }); </script> <!-- <span class="name"><span class="gost">Yahoo</span>, </span>--> <div class="test"> <table><tr> <td>3-7 Business Days $$0</td></tr></table> </div> </body> </html> So say if my string was.. Code: a = "Hello"; alert(a); How do I get it to say; Code: alert("ello"); So how do I take off the 'H' in this example.. is there a function in JS to remove a string from some text but without using the replace function? I am removing a specific string from my text using and using the replace function of the match text with a non breaking space but what i really do not want to add anything to my html. The replace is been place in a visual editor box after the user adds text and save the form, this is the code i am using to replace the code, i am using a regular expression to look for <META content="MSHTML 6.00.6000.16890" name="GENERATOR"><LINK title="/iw/ewebeditpro20/ektnormal.css" href="/iw/ewebeditpro20/ektnormal.css" rel="stylesheet"> : // Pass 1 - Removes extra code checks on Regular Expression inside replace function var newstrchanged = strEscaped.replace(/^%26%2365279%3B%20%3CMETA%20content%3D%22MSHTML%20.+.stylesheet%22%3E/g, ''); First of all, I wanna make my code using (while loop, do while loop, int, double, if, else, else if, Math.random, Scanner-- not string or boolean). -Actually the game goes like, after the user and the computer has has selections it is gonna be displayed and then tell who wins and who does not--I dont know how to do it. - When user presses 4, the the computer tells wins, loses and ties. This happens to me after two tries, wheteher i press 4 or not.(actully its |y/n| in mine and i want to change it to just 4 to quit). -The user is only allowed numbers from 1 to 4, but apparently, i add any number and it works. -My code gos like this: import java.util.Scanner; import java.util.Random; public class boobitty { public static void main (String[] args) { int Computer=0,Player=0,tie=0,compic,pscore; String str="y"; Random generate= new Random(); Scanner scan=new Scanner(System.in); while (str.equals("y")) { compic=generate.nextInt(3)+1; System.out.println ("Enter 1 for Rock, 2 for Paper, and 3 for Scissors"); pscore=scan.nextInt(); if (compic==pscore) { System.out.println("Tie Game"); tie++; } else { switch (pscore) { case 1: { if (compic==2) { System.out.println ("Paper beats Rock"); System.out.println ("Computer wins"); Computer++; } else { System.out.println ("Rock beats Scissors"); System.out.println ("Player wins!"); Player++; } break; } case 2: { if (compic==1) { System.out.println ("Paper beats Rock"); System.out.println ("Player wins!"); Player++; } else { System.out.println ("Scissors beat Paper"); System.out.println ("Computer wins!"); Computer++; } break; } case 3: { if (compic==1) { System.out.println ("Rock beats Scissors"); System.out.println ("Computer wins"); Computer++; } else { System.out.println ("Scissors beat Paper"); System.out.println ("Player wins"); Player++; } break; } default: { System.out.println("Enter 1 2 or 3"); break; } } } Scanner scen = new Scanner (System.in); System.out.println ("Play again? y/n"); str = scen.nextLine(); if(!(str.equals("y"))) { System.out.println ("Scores:"); System.out.println ("Ties= "+tie+" Wins= "+Player+" Losses= "+Computer); } else { System.out.println ("playing again"); } } } } I am trying to set up a script that looks in the code on one page, counts the number of instances of an element by ID, and then delivers that information back to another page in order to set the height for an area on that page. Context: I have a PHP page with an include statement and the included content is overflowing. Scrolls won't emerge (IMO) unless the container PHP page is told what is the height of the included content. I know almost nothing about javascript, so this is becoming a significant challenge. I cobbled together two scripts, just to get started: one that searches for occurrences of a word in a document, and another that converts that information into the height for the "div" tag in my PHP page. Obvious issues: 1) The first script will not search outside of the form for the word occurrence. I don't even want the count to be delivered in a form; I want it passed to the second script without any user involvement at all. 2) I am not sure how to set write the code so that it searches another document entirely. I bookmarked this site thinking that there might be some solutions in there. Code: <script language="Javascript"> function countInstances(string, word) { var substrings = string.split("navpic3"); return substrings.length - 1; } </script> <script type="text/javascript"> var thumbinstance=countInstances var thumbheight=[(50*thumbinstance)+300] function menuspace(images){ document.write("<div style="+'"'+"height: "+thumbheight+"px;"+'"'+">") } </script> <script type="text/javascript">menuspace(255)</script>content</div> <form> <input type="label" name="string" value="navpic3 navpic3"> <input type="button" value="count instances" onClick="document.write(countInstances(string.value))"> </form> Thanks in advance for any help. Elizabeth Hey all, I have a simple example below showing how when I pass in the value of the value attribute of option node, and then use if operator to check whether parameter is a string or not, even though it's a string, it converts it to false boolean and triggers the else statement rather than calling a function: Code: <body> <select> <option value="createMsg">Add Message</option> <option value="removeMsg">Remove Message</option> </select> </body> Code: var menu = { handleMenu : function(callback){ if(callback === "string"){ menu[callback](); } else { console.log("Issue occurred") } }, createMsg : function(){ var content = document.createTextNode("Please give additional information."), heading = document.createElement("h1"); heading.appendChild(content); document.body.appendChild(heading); }, removeMsg : function(){ } } document.getElementsByTagName('select')[0].onchange = function(){ menu.handleMenu(this.value)}; callback should be a string so why is it saying otherwise? Thanks for response I'm using jQuerys' $.ajax to load a div with some details in it and i want to use the esc key to close the div. Heres the code so far Code: $('.username span').click(function(){ $('.MiniPro').remove(); ID = $(this).parent().attr('ID'); This = $(this).parent(); miniPro(); }); function miniPro() { $(This).append('<div class="MiniPro">Loading...</div>'); $.ajax({ type: 'POST', data: 'userID='+ID, url: 'processing/miniPro.php', success: function(msg) { $('.MiniPro').remove(); $(This).append('<div class="MiniPro"'+msg+'<div class="MiniProClose">X</div></div>'); } }); } Is it even possible to do that? Hello, I'm currently using this code: I want it so that there isnt a box around the countdown, and the countdown is the same text as "you will be redirected in" here is it in action: http://www.playelite.org/redirect.html Code: <form name="redirect"> <center> <font face="Arial"><b>You will be redirected in: <form> <input type="text" size="3" border="0" name="redirect2"> </form> seconds</b></font> </center> <script> <!-- var targetURL="http://google.com" var countdownfrom=10 var currentsecond=document.redirect.redirect2.value=countdownfrom+1 function countredirect(){ if (currentsecond!=1){ currentsecond-=1 document.redirect.redirect2.value=currentsecond } else{ window.location=targetURL return } setTimeout("countredirect()",1000) } countredirect() //--> </script> Thanks Hi all, here i got a problem, i want to remove all the Li tag that with a classname"test" . Code: <html> <head> <title>Introduction to the DOM</title> <script> window.onload = function(){ var li = document.getElementsByTagName("li"); for ( var j = 0; j < li.length; j++ ) { if(li[j].getAttribute('class')=='test'){ li[j].parentNode.removeChild( li[j] ); } } }; </script> </head> <body> <h1>Introduction to the DOM</h1> <p class="test">There are a number of reasons why the DOM is awesome, here are some:</p> <ul> <li class="ppp">ppp.</li> <li class="test">It's easy to use.</li> <li class="test">It can help you to find what you want, really quickly.</li> <li class="test">It's easy to use.</li> <li class="test">It can help you to find what you want, really quickly.</li> <li class="test">It's easy to use.</li> <li class="test">It can help you to find what you want, really quickly.</li> <li class="test">It's easy to use.</li> <li class="test">It can help you to find what you want, really quickly.</li> <li class="test">It's easy to use.</li> <li class="test">It can help you to find what you want, really quickly.</li> <li class="test">It's easy to use.</li> <li class="test">It can help you to find what you want, really quickly.</li> <li class="test">It's easy to use.</li> <li class="test">It can help you to find what you want, really quickly.</li> <li class="test">It's easy to use.</li> <li class="test">It can help you to find what you want, really quickly.</li> </ul> </body> </html> Hi Guys, I'm new at JS. I need to remove TR elements from parent table but the problem is there are no table ID/Name Is it possible to perform it? Please see attach - i need remove red marked block... what scrip i have to use if i will put it to the green block? Thank you. i am doing an edit operation in my web page by changing a image, but when i am revisiting the page it is showing older image rather than new one. So can any one say how to refresh the page without using any button like window.location.reload(). Is there any way to use math.random in url?? I am using this script that gets messed up when you resize the window. I figured out that I could use this other script to do stuff just after the window gets resized. Code: (function($,sr){ // debouncing function from John Hann // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/ var debounce = function (func, threshold, execAsap) { var timeout; return function debounced () { var obj = this, args = arguments; function delayed () { if (!execAsap) func.apply(obj, args); timeout = null; }; if (timeout) clearTimeout(timeout); else if (execAsap) func.apply(obj, args); timeout = setTimeout(delayed, threshold || 100); }; } // smartresize jQuery.fn[sr] = function(fn){ return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); }; })(jQuery,'smartresize'); // usage: $(window).smartresize(function(){ // code that takes it easy... }); So I re-instantiate the misbehaving script in there so it fires whenever the window is resized (I changed it to 500 so it really only fires once after you are done resizing). This fixes it. The new instance doesn't have a problem but the old instance is still there and messed up. so I use. $($.misbehaving-script).remove(); just before instantiating after window resize, which gets rid of broken instance. Problem solved.. Except that messes up a different script! It's driving me crazy. Any advice would of course be very welcome. I have a check box right now that calls an onClick event. Here is the Input: Code: <input name="usePad" type="checkbox" value="usePad" id="usePad" onclick="showBind('padShow','padOk')" /> Here is the Script: Code: var xmlhttpshowBind; var showBindDiv; function showBind(BindPage,BindDiv) { xmlhttpshowBind=GetXmlHttpObject(); if (xmlhttpshowBind==null) { alert ("Browser does not support HTTP Request"); return; } var url="/scripts/"+BindPage+".php"; showBindDiv=BindDiv; xmlhttpshowBind.onreadystatechange=showBindGet; xmlhttpshowBind.open("GET",url,true); xmlhttpshowBind.send(null); } function showBindGet() { if (xmlhttpshowBind.readyState==4) { document.getElementById(showBindDiv).innerHTML=xmlhttpshowBind.responseText; } } It works great to fill the div called for, but I would like it to remove the div contents if it's unchecked, I'm not sure how to do this?? Any help would be appreciated! Thank you in advance! I'm trying to set the contact form on http://gspbuzz.com/test to remove the text when a user clicks it. Any idea how I can accomplish this?
Hi, I am new to development in javascript. I want to remove items in an array by passing index values. The index values may have 1 or more values. For example, i have the following array var arr1=new Array("aa","bb","cc","dd","ee","ff"); var index = new Array(); index[] = 3; index[] = 5 Using the above index values, i want the output as "aa","bb","cc","ee" when i used the slice function, i am not getting the desired output like the one above. Can someone please help me out? Thanks Raja Hi, I have a regular expression which removes anything between <script> and </script> tags. The problem comes when there is more than one on a page, eg. Code: <script>lose this</script> content I want to keep <script>lose this</script> Everything in that string is removed... how can I limit it to complete tags only? I did a search but could only find ones that either remove tags only, or have the same problem as mine. Cheers, Gus I'm working on a webpage where I used this code to display random images. I used it for three images in a row and I keep getting white spacing in between them when they should be right up against each other. This is the code I'm using: Code: <script language="JavaScript"> <!-- /* Random Image Script- By JavaScript Kit (http://www.javascriptkit.com) Over 400+ free JavaScripts here! Keep this notice intact please */ function random_imglink(){ var myimagesblue=new Array() //specify random images below. You can have as many as you wish myimagesblue[1]="images/circles_blue/circle_bl_01.jpg" myimagesblue[2]="images/circles_blue/circle_bl_02.jpg" myimagesblue[3]="images/circles_blue/circle_bl_03.jpg" myimagesblue[4]="images/circles_blue/circle_bl_04.jpg" myimagesblue[5]="images/circles_blue/circle_bl_05.jpg" myimagesblue[6]="images/circles_blue/circle_bl_06.jpg" myimagesblue[7]="images/circles_blue/circle_bl_07.jpg" myimagesblue[8]="images/circles_blue/circle_bl_08.jpg" myimagesblue[9]="images/circles_blue/circle_bl_09.jpg" myimagesblue[10]="images/circles_blue/circle_bl_10.jpg" myimagesblue[11]="images/circles_blue/circle_bl_11.jpg" var ry=Math.floor(Math.random()*myimagesblue.length) if (ry==0) ry=1 document.write('<img src="'+myimagesblue[ry]+'" border=0>') } random_imglink() //--> </script> Is there a way to get the white space removed? Thanks Code: <!-- Add-in device.js files are placed here --> <div id="deviceScript"></div> What I have been doing is clearing this out when I select a new (add-in) device and append a script tag with its .src file. I have some vague idea that unhooking an external js file might remove its code from memory. If not is there a way to do so? Functions in different script files have the same name - for example, I use start() to set up each device. It seems that the latest device start() overwrites the one in memory - but I am not sure if overwrite means delete last one or whether there is some horrendous build up of dross going on. Any clarification appreciated. Hi I have javascript which calls a load of Java applet functions, there are many of these, and the parameters are all the same. The only thing that changes is the name of the function to call. So here is an example of the Javascript: Code: function answercall() { try { document.frm.response.value = document.Telepop.Answercall(document.telepopfrm.extension.value); } catch(err) { alert(err.description); } } function clearcall() { try { document.telepopfrm.response.value = document.Telepop.Clearcall(document.telepopfrm.extension.value); } catch(err) { alert(err.description); } } function holdcall() { try { document.telepopfrm.response.value = document.Telepop.Holdcall(document.telepopfrm.extension.value); } catch(err) { alert(err.description); } } etc, etc.... and the html like this: <input type="button" value="Answer" onclick="answercall()" /> <input type="button" value="Hangup" onclick="clearcall()" /> <input type="button" value="Hold" onclick="holdcall()" /> There does seem to be a lot of duplication here, so I was wondering of ways to reduce or eliminate the duplication. any ideas? AC |