Change text of button based on attributes - javascript

i have searched stackoverflow but i did not find any answers.
Please Check below html
<button type="button" class="nacnchor" value="1">hello</button>
<button type="button" class="nacnchor" value="2">hello</button>
<button type="button" class="nacnchor" value="3">hello</button>
I can change text of button based on class
$('button.nacnchor').text("REWRITE");
But i want to change the value based on attribute value , is ther anything like $('button.nacnchor,attr(value=1)') :p .

Attribute selectors are documented here: http://api.jquery.com/attribute-equals-selector/
$("button.nacnchor[value='1']").text("REWRITE");

You can do this:
$("button[value='1']").text("REWRITE");

When seeking how to use any given library it's always best to *R*ead *T*he *F*unny *M*anual which has an entire page dedicated to this.
...for example, $("a[rel='nofollow']"), will select Some text but not <a href="example.html" rel="nofollow foe">Some text.
Attribute values in selector expressions must follow the rules for W3C CSS selectors; in general, that means anything other than a valid identifier should be surrounded by quotation marks.
double quotes inside single quotes: $('a[rel="nofollow self"]')
single quotes inside double quotes: $("a[rel='nofollow self']")
escaped single quotes inside single quotes: $('a[rel=\'nofollow self\']')
escaped double quotes inside double quotes: $("a[rel=\"nofollow self\"]")

try the below code
$("button[value='1']").text("REWRITE");

Related

replace \n from tag attribute using javascript regex with empty character

I have tag like <span style="font-size:10.5pt;\nfont-family:\nKaiTi"> and I want to replace \n within tag with empty character.
Note: Tag could be anything(not fixed)
I want regex expression to replace the same in the javascript.
You should be able to strip out the \n character before applying this HTML to the page.
Having said that, try this (\\n)
You can see it here: regex101
Edit: A bit of refinement and I have this (\W\\n). It works with the example you provided. It breaks down if you have spaces in the body of the tags (<span> \n </span>).
I've tried everything I know to do. Perhaps someone with more regex experience can assist?

Regex to replace only links without replacing the src attributes

I'm trying to replace the urls in the block of text with clickable link while rendering.
The regex am using :
/(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/ig
Example
This is the text i got from http://www.sample.com
it should be converted to
This is the text i got from
http://www.sample.com
the problem is when the text having the img tag , then the src attribute also getting replaced which i don't want.
Kindly help me to replace only direct links not the links in the src="" attributes
Thanks
Add a negative look-behind assertion at the beginning of your regex, to search only for strings not after src=":
(?<!src=")
Edit: Unfortunately look-behind assertions do not work in javascript regexes. Alternatively, you can use a negative look-ahead assertion like this:
((?!src=").{0,4})
remembering that you need to use the matched string in the replacement (otherwise you would delete 4 characters before http://).

Put a quote in a HTML attribute

example,
<span class="hotspot"
onmouseover="tooltip.show('<table id=\"test\"><tr><th>123</th></tr><tr><th>123</th></tr></table>');"
onmouseout="tooltip.hide();">porttitor orci</span>
in here, i'm trying to add id attribute in table tag, but i already used "" and '', so something is messed up and not working.
Any good solution?
<span class="hotspot"
onmouseover="tooltip.show('<table id="test"><tr><th>123</th></tr><tr><th>123</th></tr></table>');"
onmouseout="tooltip.hide();">porttitor orci</span>
JSFiddle
In an attribute value that is delimited by Ascii quotation marks ("), you can present the Ascii quotation mark itself using the reference ".
However, there is usually a better way. In the given case, you can simply omit the quotation marks, since id=test works just fine (unless you are using XHTML served with an XML content type, and most probably you aren’t).

escaped char generating "missing ) after argument list" error?

This little line of code here is from a shopping cart:
Add To Cart
Firebug's console shows: "missing ) after argument list". Clearly the ')' isn't missing! But I suspect it has something to do with the escaped char
'
since the other similarly formatted links without apostrophes in the name= argument are working fine.
Thoughts?
onclick="simpleCart.add( 'name=The Devil's Sneakers'...
Is an HTML attribute with the apostrophe escaped at an HTML level. It is exactly the same as saying:
onclick="simpleCart.add( 'name=The Devil's Sneakers'...
with the obvious problem with the string closing too early. HTML-escaping doesn't help you because the HTML-escape is only needed to encode the characters that are special to HTML. That would include a double-quote character but not a single quote, since you've used double-quotes to delimit the attribute value.
The apostrophe isn't special to HTML here, but it is to JavaScript. You need to use JavaScript string literal escaping, and in that kind of escaping you need backslashes:
onclick="simpleCart.add( 'name=The Devil\'s Sneakers'...
Either way, it's clear that escapes inside other escapes are really confusing and this is another good reason not to use inline event handler attributes. Instead, in plain JavaScript:
<button type="button" id="foo">Add to cart</button>
<script type="text/javascript">
document.getElementById('foo').onclick = function() {
simpleCart.add("name=The Devil's Sneakers", 'price=10', 'shipping=0', 'quantity=1');
};
</script>
(I used a button because what you've got isn't a link that goes anywhere. You can style it to look like a link instead of a button if you prefer, but it's better not to have a link if none of the normal affordances of links, like middle-click or right-click-bookmark make any sense.)
Escape apostrophes with \x27. This code should work:
Add To Cart
Try \' instead of '
It works well.
<html>
<body>
Add To Cart
Add To Cart
</body>
</html>

Iavascript \" \' I'm confused

I'm busy on little JavaScript item, but I got confused by the next rule:
<img src="images/icons/collapse.gif"><br/>'
It doesn't take the onclick remove map action. I don't know what I have to put between the () to make I can put text there. Does somebody have the solution?
Try just :
onclick="removeMap('test')"
if test is a variable:
onclick="removeMap(test)"
The \" is to scape the closing double quote of the onclick event. In this case I don't see any use in your calling. When used event functions in the html tags just like your onclick event, as html uses double quotes for the attributes you need to use the single quotes in your inline javascript functions.
By the way, have you checked about JQuery JavaScript Library. As you are starting coding javascript it's good to know your options.
Or just stop writing javascript inside of your HTML tags, just call functions and put your code in your functions... far much readeable !
How about:
<img src="images/icons/collapse.gif"><br/>'
You should use an editor that properly highlights strings (and what is going on should be obvious)
Escaped quotes don't have any special meaning in HTML. Instead, the browser will see your line as:
<a href="#"
onclick="removeMap('+ \"
test\
" +')"
style="float:right; margin-right: 30px;"
>
Where the onclick would be broken Javascript, and there would be two attributes in the tag (test\ and " +')") that it doesn't recognize and doesn't know what to do with.

Categories

Resources