JavaScript - Need Help With Replace Function
Hi, I need function for replacing one string with some html tag (for example <div>something</div>) in web page. Tricky is that in this replacing I must ignore all text between <a> and </a>. It's look easy but I am not good in javascript.
Similar TutorialsHi, I am facing a problem in passing replace() function as an argument in user defined java function, can any one help me how to resolve it? intention is to pass a file path to my user defined function, but before passing the path i want to replace the character '\' to '\\' I am posting my javascript function he <a href="#" onclick="OpenDocPreview('<%# Eval("PATH")%>'.replace(/\\/g,"\\\\"), '<%# Eval("Filename")%>')"><%# Eval("DocTitle") %></a> function OpenDocPreview(url, docname) { alert('message from search base : ' + url + ' ' + docname); } thank you, Hi, I'm just wandering, how can you stop the replace() function from replacing characters inside a word, such as, if I try to replace 'x' with 'and', then my string would go from 'example, x' to 'eandample, and'. Thanks in advanced Code: s = s.replace(/\[code\](.*?)\[\/code\]/gi,'<span class="code">$1</span>'); This works as expected, but what I'm trying to do is replace the content within the tags with a santized version of what was in it before. Example Code: function clean(str){ //clean up string stuff here return str; } s = s.replace(/\[code\](.*?)\[\/code\]/gi,'<span class="code">'+clean('$1')+'</span>'); But as with everything in life it's never that easy, it's simply passing "$1" to the clean function instead of what was actually within the code tags. Can anyone tell me how I'd go about doing this? I have a js replace function to replace text next to two radio buttons on a pre set form. Script is as follows. Code: document.body.innerHTML = document.body.innerHTML.replace( "Payment by <b>Moneybookers</b> e-wallet<br>", "" ); document.body.innerHTML = document.body.innerHTML.replace( "Maestro, Visa and other credit/debit cards by <b>Moneybookers</b>", "Pago con Diners Club, Mastercard o Visa" ); The script works fine in Chrome and Firefox, however, the script is not actioned in Explorer. I believe it has something to do with there being , / - within the text I am replacing? When I use the function to replace text with no , / - in the text - it works fine in explorer, however, for example when I try to replace text.. - "Maestro, Visa and other credit/debit cards by Moneybookers" this does not work in explorer.. I'm assuming because of the coma and forward slash. Honestly I've tried everything but just can not get this to work. I have no access to the HTML table where the form is posted this is why I am attempting this funtion. Any help would be greatly appreciated!! I'll probably be laughed at for this attempt of coding, but all I am trying to do is read an input field, remove the "$" and write the new value in a different input field. Code: function convert_action (form){ var input1=form.input_field1.value; var clean=input1.replace(/$/, ''); form.input_field2.value = clean; } Looks simple, and thought this would work, it kindof does, it just does not strip out the dollar sign... Help would be appreciated...greatly... i am trying to make an online graphing calculator with javascript. dont ask how because i dont know. but there is an annoying error in a do...while loop. although it should break out of the loop when the |'s (absolute value signs) are replaced with Math.abs( and ). here is the code. Code: var initec = function(){ var rg = { } ; rg.matc = false; rg.i = 0; rg.change = function(equ){ if (typeof(equ) != "string"){ alert('Equation must be a string'); return; } alert("starting equation: "+ equ); rg.i = 0; do{ rg.matc = equ.match(/\|/); if(rg.i === 0){ equ.replace(/\|/, " Math.abs("); rg.i = 1; alert("1 "+equ); } else { equ.replace(/\|/, " ) "); rg.i = 0; alert("0 "+equ); } }while(rg.matc) alert("finished equation: " + equ); } return rg; } rg=initec(); rg.change("|8/x+7|-2"); the last 2 lines and the alerts are for debugging. as you can see, it is not finished. but still, it should work. Hi I'm looking for a very simple script to replace one div with another. I've found a few but they seem overly complicated with features I don't need. I simply want to be able to click on a div and for it to be replaced with another div previously hidden. If someone could suggest a solution or point me in the right direction I'd really appreciate it. Thanks in the div class "done" it reads 13/34 i want to compare these to numbers like this if (13 < 34){do something()} else {return false;} so i am trying to get each number by it self to compare with. var did=m[1].getElementsByClassName("done")[1].innerText.replace(/\/.*/,"").value; this line returns 13 correctly. var need=m[1].getElementsByClassName("done")[1].innerText.replace(? ,"").value; this ? the info that i need to get for the other number. i know how to do it if it were a letter but can find the correct way being that it is a / instead. Thx First of all, I don't want to replace text in a form, or on click. I'm writing up a design document in HTML, and I need an area to display code. The < and > tags won't show up, obviously, but to make it easier on the programmer, I don't want him to have to manually put in < and > for each time it shows up. I am aware that JavaScript has a replace function, but I am not familiar with JavaScript quite yet. Is there a way to have JavaScript find the < and > in a certain div, and replace it with < and > on load? I would appreciate your help so much, Its been frustrating me. I found a regular expression that will strip my string of every html tag... var stripped = htmlStr.replace(/(<([^>]+)>)/ig,""); Well, I also want it to strip every thing between parentheses, square brackets, and replace " &# 160;" with a space. It would be really cool if you could explain how to do this as well, because the stuff in the above code after "replace" confuses me. I have no idea how that's working. Building a shopping cart, at the moment I have 1 variable array. My JS gets values from hidden inputs when add to shopping cart is clicked. The JS then pushes them all into the array in order. I've managed to get the js code to replace innerHTML and add the array to the HTML to show the products, quantity and totals in a table. However its only showing the last product, price, and total in the array in the table. I need to get it to do this: Product: Quantity: Total: Product1 1 1 Product2 2 2 Product3 3 3 So basically I think I need to have my 3 for statements then plus 2 to each i instead of i++ to print out the products but that still doesn't solve my problem with printing every product in a vertical list, same with quantity and same with total. Heres the code Code: var cart= new Array(); function readInput(){ var prd=document.test.prd.value; var prc=document.test.prc.value; var qty=document.test.amount.value; var total= prc * qty; cart.push( prd ) cart.push( qty ) cart.push( total ); var lenp=cart.length; for(var i = 0; i<lenp-2; i++ ) { document.getElementById("product").innerHTML = cart[i]; } for(var i = 0; i<lenp-1; i++ ) { document.getElementById("qty").innerHTML = cart[i]; } for(var i = 0; i<lenp; i++ ) { document.getElementById("total").innerHTML = cart[i]; } } Code: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="css\basic.css" /> <script language="JavaScript" src="test.js"></script> <title>Shopping Time</title> <body> <form name="test"> <input type="hidden" name="prd" value="Paper"> <p class="desc" value="Paper"> Product: Paper 1</p> <p class="desc"> <input type="hidden" name="prc" value="1.99"> Price: £1.99</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select></p> <p class="submit"> <input type="button" onclick="readInput();" value="Add To Cart" /> </p> </form> <table border="1px solid black" rules="ALL" frame="void" class="cart" width="100%"> <tr> <th> <p class="cart">Product:</p> </th> <th> <p class="cart">QTY:</p> </th> <th> <p class="cart">Total:</p> </th> </tr> <tr> <td class="test"> <div id="product"> <p class="cart">Product:</p> </div> </td> <td> <div id="qty"> <p class="cart">QTY:</p> </div> </td> <td> <div id="total"> <p class="cart">Total:</p> </div> </td> </tr> </table> </body> </html> document.write(str.replace(/(\+|\(|\)|.|-|\/ /g,'')); feeble attempt to remove '.' '(' ')' ' ' '-' '/'and '+' How would you do a .replace for a Javascript if statment.
Building a javascript shopping cart for an assignment. First time using Javascript so ignore the ****ness of it please Anyway basically I need to replace the qty of an item in an array if the item has already been added to the array. if it hasn't then it adds the normal values to the array. So I assume atm that I have to run an if statement when the function Code: function readInput(prd, qty, prc){ is called that checks in the array before pushing the new values into the array if array contains product name then var id = productarrayID+1 then replace that with the new quantity read in by the form. Quantity in my array is after the product name so I assume productarrayID+1 would give me the qty id variable. So heres the steps, be they correct: 1) get product name from HTML. 2) check if array contains product name. 3) continue with adding to array if product name doesn't exist in array. 4) if array contains product name retrieve id of product name within array. 5) + 1 to the ID to the product name thus giving me the ID for the quantity. 6) run some array.replace with the new id and replace with the quantity. Just wondering if thats a year or no to how that would run. Il post my HTML code but please dont post solutions I would prefer to write the code myself but just checking if the step-by-step process would work thankyou. HTML: Code: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="css\basic.css" /> <script language="JavaScript" src="js/basic.js"></script> <title>Shopping Time</title> <body> <div id="wrapper"> <div id="header"> </div> <div id="content"> <div id="items"> <div id="column"> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper"> <p class="desc" value="Paper"> Product: Paper 1</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper2"> <p class="desc" value="Paper2"> Product: Paper 2</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper3"> <p class="desc" value="Paper"> Product: Paper 3</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> </div> <div id="column"> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper4"> <p class="desc" value="Paper4"> Product: Paper 4</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £2.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper5"> <p class="desc" value="Paper5"> Product: Paper 5</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper6"> <p class="desc" value="Paper6"> Product: Paper 6</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> </div> <div id="column"> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper7"> <p class="desc" value="Paper5"> Product: Paper 7</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper8"> <p class="desc" value="Paper8"> Product: Paper 8</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper9"> <p class="desc" value="Paper9"> Product: Paper 9</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> </div> </div> <div id="sidebar"> <table border="1px solid black" id="table" rules="ALL" frame="void" class="cart" width="100%"> <tbody> <tr> <th> <p class="cart">Product:</p> </th> <th> <p class="cart">QTY:</p> </th> <th> <p class="cart">Price:</p> </th> <th> <p class="cart">Total:</p> </th> </tr> </tbody> </table> <button onclick="viewshoppingcart('table')" class="button">Update Shopping Cart!</button> </div> </div> <div id="footer"> </div> </body> </html> Script: Code: var cart= new Array(); var down= 0; function readInput(prd, qty, prc){ down = down + 1; var total= prc * qty; cart.push( prd ) cart.push( qty ) cart.push( prc ) cart.push( total ); } var p = 0; var q = 1; var t = 2; var s = 3; function viewshoppingcart(id){ if(!cart[p])return; for(var i = 0; i < down; i++ ) { var tbody = document.getElementById(id).getElementsByTagName("tbody")[0]; var row = document.createElement("tr") var data1 = document.createElement("td") data1.appendChild(document.createTextNode( cart[p] ) ) var data2 = document.createElement("td") data2.appendChild (document.createTextNode( cart[q] )) var data3 = document.createElement("td") data3.appendChild (document.createTextNode( cart[t] )) var data4 = document.createElement("td") data4.appendChild (document.createTextNode( cart[s] )) row.appendChild(data1); row.appendChild(data2); row.appendChild(data3); row.appendChild(data4); tbody.appendChild(row); p = p + 4; q = q + 4; t = t + 4; s = s + 4; } down = 0; } Code: dropDownPart.innerHTML = tmpInnerHTML; how does this tmpInnerHTML gets filled : Code: for (var i = 0; i < arr.length ; i++) { var arr1 = arr[i].split('|'); id_tezaver = arr1[0]; term = arr1[1] // if arr1[1] contains ', then ewerything after that disapears inside term, // like "bird's eyes" becomes "bird" // how to handle that ? // bad example how I get to verify that the stuff is complete inside array // term = arr1[1].replace(/'/i, /oo/); } Hi all, I have this code: Code: <input type="hidden" value="update_cart" name="ca"> <input type="hidden" value="2" name="numcart"> <input type="submit" name="submit" value="Update Cart" class="inpSubmit"> I'd like to replace this: Code: <input type="submit" name="submit" value="Update Cart" class="inpSubmit"> with this: Code: <button>blabla<button> How can I do? I know something about unobtrusive javascript but I don't know how to do it. Thank you very much I have a javascript that takes information from a form and recompiles that information into a readable shift report. The problem I am having is with returns in the textareas. I tried: Code: theReport = theReport.replace(" ","<br>" but that space puts the rest of the var on a different line and screws with the results. How can I replace a return with <br>? Does anyone know if it it possible to automatically replace any instance of & with 'and' using javascript when someone types into a text input?
I know how the replace method works, but I can't for the life of me figure out exactly what this replace function is doing... Can someone shed somelight for me? the g I *think* is a flag for global replacement... Code: replace(/\s/g, " "); I know that *something* is being replaced by a non-blank space, but what? |