Daniel Gustafsson
Daniel Gustafsson

Reputation: 1817

call method on textbox if textbox is on a specific page

I have a method that is set dynamically to different textboxes in my form. But the problem is that i only want the method to work if i am on my view called riskscore.cshtml. It there a way? like a if(page == riskscore.cshtml){ do method} kind of code?

Upvotes: 0

Views: 65

Answers (3)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123473

JavaScript typically has no way of knowing if or when a particular view file was used server-side. It only knows the results that the view rendered.

You can wrap the contents of the view in say a <div class="riskscore">, then select textboxes within those:

$('.riskscore :text')...

You also mentioned in a comment that other elements won't exist without this view. You can use them as your condition, checking whether they exist:

if ($('.other-elements').length) {
    // do method
}

Replace '.other-elements' as needed.

Upvotes: 2

Sonu Sindhu
Sonu Sindhu

Reputation: 1792

you can use window.location to get the current location. or you can use location.pathname to current path.

link here

I think it will help

Upvotes: 1

Ezekiel Victor
Ezekiel Victor

Reputation: 3876

You can test URLs in JavaScript:

if(/\/riskscore\.chtml$/.test(window.location.pathname)) {
  // You're on riskscore.chtml... Do something
}

Upvotes: 2

Related Questions