String attribute in Razor as a part of onclick confirmation - javascript

I need a confirm window before calling the action specified in Url.Action. My code is:
<a href="#Url.Action("Delete", "OriginalWorks", new {id = w.Id})"
onclick="return confirm('Do you... '+ #w.Title + '?')"><span></span></a>
Where w is the instance of the OriginalWork class. If I look into HTML of the site, there is the right string value in confirm, but the window doesn't show and the action is called immediately. If a do the same with an int attribute, for example Id:
<a href="#Url.Action("Delete", "OriginalWorks", new {id = w.Id})"
onclick="return confirm('Do you... '+ #w.Id + '?')"><span></span></a>
It works as expected, showing a message in an confirm window with an value of Id.
I tried Title.toString(), but it doesn't help. I think it would work with Html.ActionLink, but I want to use this because of span in a a element.
Any ideas? Thanks a lot.

Try to use HttpUtility.HtmlEncode for your w.Title to awoid escape chars that can break javascript

Thanks for help. Solved it now, I didn't realize that the w.Title value was evaluated as a variable (and JavaScript maybe tried to find a variable called like a Title value), not as a string value. It simply works like this:
<a href="#Url.Action("Delete", "OriginalWorks", new {id = w.Id})"
onclick="return confirm('Do you... #w.Title?')"><span></span></a>
So there is an expected string value of Title.

Related

How to find a varible html element executing javascript in python

I'm trying to use 2Captcha service to solve an h captcha V2.
Works like this:
you get a value to solve the captcha
Then you find a textarea element in the HTML code to insert that value (here's my problem)
you insert the value in that element
You press submit button and the captcha is solved
First I'm going to present a working example, then I'll present where I have the problem.
This is the HTML code to find and insert the obtained value:
textarea id="h-captcha-response" name="h-captcha-response" style="display: none;"></textarea>
This is the python code used to insert the value:
value = get_value()
insert_solution = 'document.getElementById("h-captcha-response").innerHTML="' + value + '";'
driver.execute_script(insert_solution)
What this exactly does is taking you from this:
and this is the result:
Finally you press the submit button and it's done. This example works
This is my problem:
In my case the HTML document has a variable ID, like this one:
<textarea id="h-captcha-response-0tesbrpxsk8" name="h-captcha-response" style="display: none;"></textarea>
Notice that the id has an alphanumerical part (0tesbrpxsk8) that always changes making it more difficult to select.
I tried to find some regular expression to use inside of document.getElementById()
With no success
I also tried to use:
document.getElementByTagName("textarea").innerHTML=".....
I'm stucked here and tried other approaches with no success because I probably because I don't implement well those solutions or they just don't work.
I'll appreciate some insights, thanks
This will fill out all of those (recaptcha / hcaptcha):
driver.execute_script('''
let [captcha] = arguments
[...document.querySelectorAll('[name="h-captcha-response"],[name="g-recaptcha-response"]')].map(el => {
el.innerHTML = captcha
})
''', value)
Try this:
const textarea = document.querySelector('[id^="h-captcha-response-"]')
textarea.value = "This is inside the textarea!"
<textarea id="h-captcha-response-0tesbrpxsk8" name="h-captcha-response"></textarea>
First of all: You set the value of an textarea with textarea.value = "some value"
You should use document.querySelector() to select elements. (You have much more abilities there)
You can select id starting with, with this query: [id^="start"]

Escape quotes in Javascript function

I'm trying to use a href onclick event in kendo grid template. When I click on the link I need the alert to diplay path text but it gives "PDF undefined error". I think it could be an issue with escape quotes.
${PDF} returns a string value.
template: "<a id='${PDF}' class='clsPDF' onclick='setpdf(\${PDF});' href='\\#'>View</a>"
<script>
function setpdf(path)
{
alert(path);
}
</script>
I would suggest slightly different approach. Instead of using inline function you can use a delegate function attached to your Grid element which will take care of all buttons like the one you defined in the template.
e.g.
$("#gridName").on("click", ".clsPDF" , function(){
var model = $("#gridName").data("kendoGrid").dataItem($(this).closest("tr"));
alert('you clicked on item with id' + model.TheIdProperty);
})
I hope this gives you the idea. I think it is cleaner this way.
When the browser looks at the link make sure it sees it like this:
<a id='someId' class='clsPDF' onclick='setpdf("pdf.pdf");' href='#'>View</a>
If it sees it like this:
<a id='someId' class='clsPDF' onclick='setpdf(pdf.pdf);' href='\\#'>View</a>
It will think pdf is a javascript object/variable and try and use it.
So you are right it is most likely a problem with quotes. You could try wrapping your \${PDF} with escaped double quotes:
\"\${PDF}\"

Popup in new window opens in new tab instead

I have a calendar created with php. In this calendar I have load_id's listed by date. I made a popup to show more data for each load. I want it to show as a popup not in a new tab. I have tried doing it as a function but as I don't know javascript I could not figure out how to pass the load_id to the url. If I replace "this.href" with the url in a function it works like a charm. But again I don't know how to pass the load id as a variable so that won't work for me. If someone could point out where I made my mistake passing the variable I would be much obliged!
This is what I have now:
echo "<span class='load_shipped'>
<a href='cal_popup.php?load_id=".$load_id."'
onclick='window.open(this.href, '_blank','left=100,top=100,width=500,height=500,toolbar=1,resizable=0');'>
".$load_id."
</a>
<br>
</span>\n";
I tried this as well to no avail:
echo "<span class='load_shipped'>
<a onclick='popup(".$load_id.");'>".$load_id."</a><br>
</span>\n";
Javascript:
<script>
function popup(id){
window.open('cal_popup.php?load_id=id', '_blank','left=100, top=100,width=500,height=500,toolbar=1,resizable=0');
}
</script>
All this did though was pass 'id' instead of the real load_id.
Seems like you're having trouble because you're using the same quotes to wrap the attribute onclick and to define the parameters of the window.open call. Try using double quotes on the attribute. You'll also need to return false; at the end of the onclick to prevent the link working as normal.
echo "<span class='load_shipped'>
<a href='cal_popup.php?load_id=".$load_id."'
onclick=\"window.open(this.href, '_blank','left=100,top=100,width=500,height=500,toolbar=1,resizable=0');return false;\">
".$load_id."
</a>
<br>
</span>\n";
In your last example where you have the function popup - please note that you need to concatenate the id variable into the string like 'cal_popup.php?load_id=' + id.

jQuery get value of each A tag without refresh the page

i need you'r help..to get each A tag href value .
E.G:
<a id="#founditems" href="45S4#6##5463&">press on me</a>
<a id="#founditems" href="ssdf#%dfd##Df">press on me</a>
<a id="#founditems" href="ghfAS3#SDF%^f">press on me</a>
<a id="#founditems" href="DFGDF#%^SDFFG">press on me</a>
I want to get each value of A tag when i press on each of one of them.
This list coming from AJAX with PHP with foreach , now in my client side when i press on "press on me" i want to take his value and to do somthing with it .
So far i did :
$(document).on('click','#founditems',function(e){
$('#founditems').each(function() {
alert($(this).attr('href'));
});
});
The alert keep give only the first A href value.
I am newbie at javascript/jquery anyone can help me ?
PS: If you got any better way to take this value , it will be nice to get advice..i prefer take this value with php, but i using AJAX to take this value from php file with foreach, so the page not refreshed.
Thanks.
Instead of #ID(founditems) change it to class attribute and you can loop it like following,
$('.founditems').each(function() {
alert($(this).attr('href'));
});
all the best.
in case you want only clicked item's href
$(document).on('click','#founditems',function(e){
var $this=$(this)
alert($this.attr('href'));
});
id is a unique value. you should select all a element by:
$('a')

Why can I not set my data attribute with jQuery?

I have the following:
editCity: "/Admin/Citys/Edit?pk=0001I&rk=5505005Z"
$('#editCity')
.attr('title', "Edit City " + rk)
.data('disabled', 'no')
.data('href', editCity)
.removeClass('disabled');
When I check the HTML with developer tools I see this:
<div class="button dialogLink" id="editCity"
data-action="EditCity" data-disabled="yes"
data-entity="City" title="Edit City 5505005Z" ></div>
Everything is updated except the href. Anyone have an ideas what I am doing wrong?
Use
var editCity = "/Admin/Citys/Edit?pk=0001I&rk=5505005Z";
What you did was a labeled statement, consisting only of a string literal and missing a semicolon.
Btw, jQuery's .data() method is not to be used for data-attributes, but just for associating JS objects with DOM elements.
I think jQuery stores the data internally if they don't exist the first time you set them. If you really want to force it:
$("#editCity").attr("data-href",editCity)
You cannot set a href attribute to a div.
you could use data-href instead, or use a a-tag instead of a div.

Categories