I am not developer so I need help from you guys.
My Problem is simple.
I just want javascript to hide word before "."
Example :
say for google.com
I just want word ".com" tobe print.
Please note that my content is dynamic so google.com will keep changing everytime to google.net or yahoo.com...... so on..
Thanx in advanced.
Well, you didn't mention quite a lot, like how do you get your input? What to do if you have no dot, are many dots?
One simple solution is:
var s = 'before.after';
var pos = s.indexOf('.');
if(pos >= 0) // here, if I don't find a dot, keep s as it is.
s = s.slice(pos);
alert(s); // .after
Here's a tutorial for you on how to parse URL's in Javascript.
http://java-programming.suite101.com/article.cfm/how_to_get_url_parts_in_javascript
If the content you want to modify is in HTML content, the easiest way is to do string replace.
http://www.bradino.com/javascript/string-replace/
Before using replace, you should first locate the content, by its container ID or Name.
http://www.tizag.com/javascriptT/javascript-getelementbyid.php
http://www.eggheadcafe.com/community/aspnet/3/43154/getelementbyname.aspx
Hope this helps.
Related
I am currently programming a forum using only javascript (No JQuery please). I am doing very well, however, there is one issue I would love help with.
Currently I am getting the post from a database, assigning it to variable MainPost, and then attaching it to a div via a text node:
var theDiv = document.getElementById("MainBody");
var content = document.createTextNode(MainPost);
theDiv.appendChild(content);
This is working quite well, however, I would LOVE to be able to do this:
document.getElementById("MainBody").innerHTML += MainPost;
But I know this would allow people to use ANY html tag they want, even something like "script" followed by javascript code. This would be bad for business, obviously, but I do like the idea of allowing posters to use the "img" tag as well as the "a href" tags. Is there a way to somehow disable all tags except these two for the innerHTML?
Thank you all so much for any help you can offer.
Ok, the first thought that came to my mind when I read this question was to find a regular expression to exclude a specific string in a word. Simple search gave a lot of results from SO.
Starting point - To remove all the HTML tags from a string (from this answer):
var regex = /(<([^>]+)>)/ig
, body = "<p>test</p>"
, result = body.replace(regex, "");
console.log(result);
To exclude a string you would do something like this (again from all the source mentioned above):
(?!StringToBeExcluded)
Since you want to exlcude the <a href and <img tags. The suitable regex in your case could be:
(<(?![\/]?a)(?![\/]?img)([^>]+)>)
Explanation :
Think of it as three capturing groups in succession:
(?![\/]?a) : Negative Lookahead to assert that it is impossible to match the regex containing the string "a" prefixed by zero or one backslashes (Should take care of the a href tags)
(?![\/]?img) : Same as 1, just here it looks for the string "img". I don't know why I allowed the </img> tag. Yes, <img> doesn't have a closing tag. You could remove the [\/]? bit from it to fix this.
([^>]+) : Makes sure to not match > zero or one times to take care of tags that have opening and closing tags.
Now all these capture groups lie between < and >. You might want to try a regex demo that I've created incorporating these three capture groups to take care of ignoring all HTML elements except the image and link tags.
Sidenote - I haven't thoroughly given this regex a try. Feel free to play around with it and tweak it according to your needs. In any case, I hope this gets you started in the right direction.
I'm new to coding and have to make a "madlib" game for class. It just needs to be basic. I need the user to fill in 4 words and replace those words for any random ones I choose in a poem or short story. The problem I'm having it that I need the poem/short story to be in the body of web page after it has been "madlibbed".
I'm really not sure how I can take some JavaScript from the head and insert it into the body. Here's what I have so far and I'll try to be specific in what my thoughts/confusions are. Don't worry if there are specific little errors in the code so far, I can figure that out later.
<!doctype html>
<html>
<meta charset="utf-8">
<title>Mad Lib</title>
<script type="text/javascript">
function myFunction()
{ var noun=prompt(“Type a noun here”);
var noun2=prompt(“Type a noun here”);
var verb=prompt(“Type a verb here”);
var verb2=prompt(“Type a verb here”);}</script>
</head><body>
<p>sad indeed, there are other (noun) still freedom from (noun2) no ideas
, from this dryness, freedom from having (verb) as a poet on forced (verb 2)</p>
</body></html>
So basically I'm trying to declare variables in the script and have users enter their answers as a prompt. I'm then trying to take their answers and insert them into the body of the paragraph where I've put (noun) (noun2) (verb) and (verb2). I'm sorry this is so elementary and I can't figure this out. I don't expect you to write the whole code out for me, you can just give me conceptual advice.
Also if you're having problems non-coding related that I might be able to help with I could try to help you with that :D
You could store the text in a variable and use the string replace function to get your content in:
var input = prompt("word");
var text = "My text PLACEHOLDER1";
var newText = text.replace("PLACEHOLDER1", input);
// render text to dom
document.getElementById('myId').innerText = newText;
More advanced would be usage of a regular expression or a template engine.
I don't like giving direct answers when it's obviously for class, so I'll try to point you to a few helpful starting places.
First, you are going to want the user to be able to enter the required verbs, nouns, etc. So, setup a form that has several text inputs.
Next, I would put a button that does a couple things when clicked. First, it would check to make sure none of the text boxes are empty (we want the person to participate, after all). Then, the button will get the input from the user and insert it into the quote where needed. Finally, it will output the whole quote. To do this, I would create a function that, when the user clicks, would write to the p tag something like this:
"sad indeed, there are other " + noun1 + " still freedom from " + noun2 +
"no ideas, from this dryness, freedom from having "...etc.
My final hint: You can update the value of the p tag (I say 'p' because that is what you mentioned earlier. Personally, I would make it a div) if you give it an ID and reference that ID like this: http://www.w3schools.com/jsref/prop_text_value.asp (under more examples, you'll see how to set that to the variables noun1 and noun2).
Leave a comment if you need more hints.
I wanto to insert a space at the start of my string
Example:
MySite
MySite (I want this)
I have tried in this mode but nothing
txt=" "+v.text();
Can someone help me? Thanks a lot
Try :
txt=" "+v.text();
The other good solution would be to use div with some style set on it.
<div style='margin-left:15px' id='main_text'></div>
You could use jQuery to place txt in the div.
txt= v.text();
$('#main_text').html(txt);
A solution is to fill the spaces with entities, but if you have to achieve this effect just for styling/visualization purpose, then wrap that line in a proper tag and use some CSS instead (e.g. text-indent property)
if you have to indent text inside a select (as I guess reading your previous answer) you may want to wrap your options inside an optgroup element (and give it some padding)
var str2 = " Here goes your string with spaces";
There are 2 solutions below :-
1. txt="<pre> </pre>"+v.text();
2. txt="<span style='margin-left:15px'></span>"+v.text();
I know it sounds odd, but I'd like to create an html class that will change the font of the last character in the tagged text.
while css lets you change the fontsize, color of the first character of some strings, there is sadly nothing to change the font of the last character.
Is it best to use the charAt(length-1) in JS or is there some way in JQuery to alter the last character of the selected class?
You'll have to fetch, change, and then reput the html content of your texts.
I made a fiddle to illustrate it :
http://jsfiddle.net/dystroy/3maG5/
Be aware that there are risks : without greater analysis you may have problems if your paragraphs (or other texts) end with an element.
EDIT : I just see that eyurdakul made a few seconds before me a very similar answer. I think he's correct too. I let this answer as a complementary one to give a testable example. Of course, if one answer should be accepted, it's probably eyurdakul's one, he was faster :)
give them some class for the selector, .last for example;
$(".last").each(function(){
var inner = $(this).html();
var firstPart = inner.substring(0, (inner.length-1));
var secondPart = inner.substring((inner.length-1), inner.length);
$(this).html(firstPart);
$("<span/>").css("font", "Arial").html(secondPart).appendTo($(this));
});
Please take a look here: http://www.binarymark.com/Products/FLVDownloader/order.aspx
What I am trying to do is to get rid of the prices inside the option tag. On that page you can see a drop-down box under Order Information, Product. I want to remove the prices from all the options that contain them in that box, so get rid of " - $75.98" for example. I am not used to JQuery, but I realize it would be possible - just not sure how to do it, so your help would be greatly appreciated.
Thanks.
George
Something like this should do the trick:
$('select[name="contractId"] > option').each(function ()
{
var $this = $(this);
$this.text($this.text().split(/\s-/)[0]);
});
That should split the text into an array with the "wanted" part as index 0, and set the text to whatever is contained in that index. You could also use a replace regex if you wanted to.
It would make more sense to do this server-side really, if a user has JS disabled on their machine you could run into problems with displaying incorrect prices.
Unfortunately based on your country of origin, Plimus is not allowed to continue this process. So I cant help you! :)
but this is the general idea:
if ($('#field > div.field-item:contains("someText")').length > 0) {
$("#somediv").addClass("thisClass");
}